From 058adc7f004ac474ba5fd58600b5ac1f422c4659 Mon Sep 17 00:00:00 2001 From: Alex Skrypnyk Date: Mon, 9 Dec 2019 21:20:02 +1100 Subject: [PATCH] Refactored path insert and update code. --- minisite.module | 113 ++++++++++-------- src/UrlBag.php | 3 +- tests/src/Functional/MinisiteTestBase.php | 24 +++- .../UploadBrowseAliasPathautoTest.php | 6 +- .../src/Functional/UploadBrowseAliasTest.php | 11 +- tests/src/Functional/UploadBrowseTest.php | 18 +-- 6 files changed, 99 insertions(+), 76 deletions(-) diff --git a/minisite.module b/minisite.module index 670ade6..9097f76 100644 --- a/minisite.module +++ b/minisite.module @@ -100,26 +100,22 @@ function minisite_validate_archive(FileInterface $file, $content_extensions) { * Implements hook_path_insert(). */ function minisite_path_insert($path) { - $params = Url::fromUri("internal:" . $path['source'])->getRouteParameters(); - $entity_type = key($params); - $entity = \Drupal::entityTypeManager()->getStorage($entity_type)->load($params[$entity_type]); - - if ($entity) { - /** @var \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager */ - $entity_field_manager = Drupal::service('entity_field.manager'); - /** @var \Drupal\Core\Field\FieldDefinitionInterface[] $minisite_fields */ - $minisite_fields = array_filter($entity_field_manager->getFieldDefinitions($entity->getEntityTypeId(), $entity->bundle()), function (FieldDefinitionInterface $field_definition) { - return $field_definition->getType() == 'minisite'; - }); - - /** @var \Drupal\Core\Entity\FieldableEntityInterface $entity */ - foreach ($minisite_fields as $minisite_field) { - if ($entity->hasField($minisite_field->getName())) { - $minisite = Minisite::createInstance($entity->{$minisite_field->getName()}); - if ($minisite) { - $minisite->setAlias($path['alias']); - $minisite->save(); - } + $entity = _minisite_get_entity_from_path($path['source']); + + if (!$entity) { + return; + } + + $path_alias = isset($path['alias']) ? $path['alias'] : ''; + + $minisite_fields = _minisite_get_fields($entity); + + foreach ($minisite_fields as $minisite_field) { + if ($entity->hasField($minisite_field->getName())) { + $minisite = Minisite::createInstance($entity->{$minisite_field->getName()}); + if ($minisite) { + $minisite->setAlias($path_alias); + $minisite->save(); } } } @@ -129,53 +125,66 @@ function minisite_path_insert($path) { * Implements hook_path_update(). */ function minisite_path_update($path) { - $params = Url::fromUri("internal:" . $path['source'])->getRouteParameters(); - $entity_type = key($params); - $entity = \Drupal::entityTypeManager()->getStorage($entity_type)->load($params[$entity_type]); - - if ($entity) { - /** @var \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager */ - $entity_field_manager = Drupal::service('entity_field.manager'); - /** @var \Drupal\Core\Field\FieldDefinitionInterface[] $minisite_fields */ - $minisite_fields = array_filter($entity_field_manager->getFieldDefinitions($entity->getEntityTypeId(), $entity->bundle()), function (FieldDefinitionInterface $field_definition) { - return $field_definition->getType() == 'minisite'; - }); - - /** @var \Drupal\Core\Entity\FieldableEntityInterface $entity */ - foreach ($minisite_fields as $minisite_field) { - if ($entity->hasField($minisite_field->getName())) { - $minisite = Minisite::createInstance($entity->{$minisite_field->getName()}); - if ($minisite) { - $minisite->setAlias($path['alias']); - $minisite->save(); - } + minisite_path_insert($path); +} + +/** + * Implements hook_entity_delete(). + */ +function minisite_entity_delete(EntityInterface $entity) { + $minisite_fields = _minisite_get_fields($entity); + + /** @var \Drupal\Core\Entity\FieldableEntityInterface $entity */ + foreach ($minisite_fields as $minisite_field) { + if ($entity->hasField($minisite_field->getName())) { + $minisite = Minisite::createInstance($entity->{$minisite_field->getName()}); + if ($minisite) { + $minisite->delete(); } } } } /** - * Implements hook_entity_delete(). + * Get Minisite fields from the entity. + * + * @param \Drupal\Core\Entity\EntityInterface $entity + * Entity to get fields from. + * + * @return array + * Array of Minisite field names. */ -function minisite_entity_delete(EntityInterface $entity) { +function _minisite_get_fields(EntityInterface $entity) { if (!$entity instanceof FieldableEntityInterface) { - return; + return []; } /** @var \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager */ $entity_field_manager = Drupal::service('entity_field.manager'); + /** @var \Drupal\Core\Field\FieldDefinitionInterface[] $minisite_fields */ - $minisite_fields = array_filter($entity_field_manager->getFieldDefinitions($entity->getEntityTypeId(), $entity->bundle()), function (FieldDefinitionInterface $field_definition) { + return array_filter($entity_field_manager->getFieldDefinitions($entity->getEntityTypeId(), $entity->bundle()), function (FieldDefinitionInterface $field_definition) { return $field_definition->getType() == 'minisite'; }); +} - /** @var \Drupal\Core\Entity\FieldableEntityInterface $entity */ - foreach ($minisite_fields as $minisite_field) { - if ($entity->hasField($minisite_field->getName())) { - $minisite = Minisite::createInstance($entity->{$minisite_field->getName()}); - if ($minisite) { - $minisite->delete(); - } - } +/** + * Get entity from provided path. + * + * @param string $path + * Source path to find the entity. + * + * @return \Drupal\Core\Entity\FieldableEntityInterface|null + * Found entity or NULL if the entity was not found. + */ +function _minisite_get_entity_from_path($path) { + try { + $params = Url::fromUri('internal:' . $path)->getRouteParameters(); } + catch (\InvalidArgumentException $exception) { + return NULL; + } + $entity_type = key($params); + + return \Drupal::entityTypeManager()->getStorage($entity_type)->load($params[$entity_type]); } diff --git a/src/UrlBag.php b/src/UrlBag.php index 02d3eac..1aeb3c7 100644 --- a/src/UrlBag.php +++ b/src/UrlBag.php @@ -144,7 +144,8 @@ public function setAlias($alias) { /** * Set parent alias. * - * This sets the "prefix" part of the alias. + * This sets the "prefix" part of the alias. Note that this can be empty + * to unset the parent part of the alias. * * @param string $parent_alias * Relative or absolute path. diff --git a/tests/src/Functional/MinisiteTestBase.php b/tests/src/Functional/MinisiteTestBase.php index 0374082..1df027e 100644 --- a/tests/src/Functional/MinisiteTestBase.php +++ b/tests/src/Functional/MinisiteTestBase.php @@ -468,7 +468,29 @@ public function getUploadedArchiveFile(Node $node, $field_name) { /** * Helper to browse fixture pages. */ - public function browseFixtureMinisite($alias, $description, $assets_paths) { + public function browseFixtureMinisite($node, $description) { + // Visit node and start browsing minisite. + $this->drupalGet('node/' . $node->id()); + $this->assertResponse(200); + $this->assertLink($description); + $this->clickLink($description); + + // Brose minisite pages starting from index page. + $this->assertText('Index page'); + $this->assertLink('Go to Page 1'); + $this->clickLink('Go to Page 1'); + + $this->assertText('Page 1'); + $this->assertLink('Go to Page 2'); + $this->clickLink('Go to Page 2'); + + $this->assertText('Page 2'); + } + + /** + * Helper to browse aliased fixture pages. + */ + public function browseFixtureMinisiteAliased($alias, $description, $assets_paths) { $this->drupalGet($alias); $this->assertResponse(200); diff --git a/tests/src/Functional/UploadBrowseAliasPathautoTest.php b/tests/src/Functional/UploadBrowseAliasPathautoTest.php index c858c6d..4c3b4f8 100644 --- a/tests/src/Functional/UploadBrowseAliasPathautoTest.php +++ b/tests/src/Functional/UploadBrowseAliasPathautoTest.php @@ -63,7 +63,7 @@ public function testUploadAndBrowsingAlias() { // Browse fixture minisite using Pathauto-generated alias. $node_alias = $node->path->get(0)->getValue()['alias']; - $this->browseFixtureMinisite($node_alias, $minisite_description, $test_archive_assets); + $this->browseFixtureMinisiteAliased($node_alias, $minisite_description, $test_archive_assets); // Disable pathauto, update node's alias and assert that update has been // applied. @@ -75,7 +75,7 @@ public function testUploadAndBrowsingAlias() { $this->drupalPostForm("node/$nid/edit", $edit, $this->t('Save')); // Browse fixture minisite using updated manual alias. - $this->browseFixtureMinisite($node_alias_updated, $minisite_description, $test_archive_assets); + $this->browseFixtureMinisiteAliased($node_alias_updated, $minisite_description, $test_archive_assets); // Enable pathauto and assert that re-generated path alias has been // applied. @@ -87,7 +87,7 @@ public function testUploadAndBrowsingAlias() { $this->assertEntityAliasExists($node); // Browse fixture minisite using updated Pathauto-generated alias. - $this->browseFixtureMinisite($node_alias, $minisite_description, $test_archive_assets); + $this->browseFixtureMinisiteAliased($node_alias, $minisite_description, $test_archive_assets); // Delete node. $this->drupalPostForm("node/$nid/delete", [], $this->t('Delete')); diff --git a/tests/src/Functional/UploadBrowseAliasTest.php b/tests/src/Functional/UploadBrowseAliasTest.php index 080f229..354a9eb 100644 --- a/tests/src/Functional/UploadBrowseAliasTest.php +++ b/tests/src/Functional/UploadBrowseAliasTest.php @@ -54,7 +54,7 @@ public function testUploadAndBrowsingAlias() { // Browse fixture minisite using manually provided alias. $node_alias = $node->path->get(0)->getValue()['alias']; - $this->browseFixtureMinisite($node_alias, $minisite_description, $test_archive_assets); + $this->browseFixtureMinisiteAliased($node_alias, $minisite_description, $test_archive_assets); // Updated node's alias and assert that update has been applied. $node_alias_updated = '/a' . $this->randomMachineName(); @@ -64,7 +64,14 @@ public function testUploadAndBrowsingAlias() { $this->drupalPostForm("node/$nid/edit", $edit, $this->t('Save')); // Browse fixture minisite using updated manually provided alias. - $this->browseFixtureMinisite($node_alias_updated, $minisite_description, $test_archive_assets); + $this->browseFixtureMinisiteAliased($node_alias_updated, $minisite_description, $test_archive_assets); + + // Remove node's alias and assert that update has been applied. + $edit = [ + 'path[0][alias]' => '', + ]; + $this->drupalPostForm("node/$nid/edit", $edit, $this->t('Save')); + $this->browseFixtureMinisite($node, $minisite_description); // Delete node. $this->drupalPostForm("node/$nid/delete", [], $this->t('Delete')); diff --git a/tests/src/Functional/UploadBrowseTest.php b/tests/src/Functional/UploadBrowseTest.php index 53e1637..9a4dde2 100644 --- a/tests/src/Functional/UploadBrowseTest.php +++ b/tests/src/Functional/UploadBrowseTest.php @@ -47,23 +47,7 @@ public function testUploadAndBrowsing() { $this->assertMinisiteUploaded($node, $field_name, $test_archive_assets); $test_archive = $this->getUploadedArchiveFile($node, $field_name); - - // Visit node and start browsing minisite. - $this->drupalGet('node/' . $node->id()); - $this->assertResponse(200); - $this->assertLink($test_archive->getFilename()); - $this->clickLink($test_archive->getFilename()); - - // Brose minisite pages starting from index page. - $this->assertText('Index page'); - $this->assertLink('Go to Page 1'); - $this->clickLink('Go to Page 1'); - - $this->assertText('Page 1'); - $this->assertLink('Go to Page 2'); - $this->clickLink('Go to Page 2'); - - $this->assertText('Page 2'); + $this->browseFixtureMinisite($node, $test_archive->getFilename()); // Delete node. $this->drupalPostForm("node/$nid/delete", [], $this->t('Delete'));