diff --git a/core/modules/system/src/Tests/Menu/AssertBreadcrumbTrait.php b/core/modules/system/src/Tests/Menu/AssertBreadcrumbTrait.php new file mode 100644 index 0000000..737a362 --- /dev/null +++ b/core/modules/system/src/Tests/Menu/AssertBreadcrumbTrait.php @@ -0,0 +1,103 @@ +drupalGet($goto); + } + $this->assertBreadcrumbParts($trail); + + // Additionally assert page title, if given. + if (isset($page_title)) { + $this->assertTitle(strtr('@title | Drupal', array('@title' => $page_title))); + } + + // Additionally assert active trail in a menu tree output, if given. + if ($tree) { + $this->assertMenuActiveTrail($tree, $last_active); + } + } + + /** + * Assert that a trail exists in the internal browser. + * + * @param array $trail + * An associative array whose keys are expected breadcrumb link paths and + * whose values are expected breadcrumb link texts (not sanitized). + */ + protected function assertBreadcrumbParts($trail) { + // Compare paths with actual breadcrumb. + $parts = $this->getBreadcrumbParts(); + $pass = TRUE; + // There may be more than one breadcrumb on the page. If $trail is empty + // this test would go into an infinite loop, so we need to check that too. + while ($trail && !empty($parts)) { + foreach ($trail as $path => $title) { + $url = _url($path); + $part = array_shift($parts); + $pass = ($pass && $part['href'] === $url && $part['text'] === String::checkPlain($title)); + } + } + // No parts must be left, or an expected "Home" will always pass. + $pass = ($pass && empty($parts)); + + $this->assertTrue($pass, format_string('Breadcrumb %parts found on @path.', array( + '%parts' => implode(' » ', $trail), + '@path' => $this->getUrl(), + ))); + } + + /** + * Returns the breadcrumb contents of the current page in the internal browser. + */ + protected function getBreadcrumbParts() { + $parts = array(); + $elements = $this->xpath('//nav[@class="breadcrumb"]/ol/li/a'); + if (!empty($elements)) { + foreach ($elements as $element) { + $parts[] = array( + 'text' => (string) $element, + 'href' => (string) $element['href'], + 'title' => (string) $element['title'], + ); + } + } + return $parts; + } + +} diff --git a/core/modules/system/src/Tests/Menu/AssertMenuActiveTrailTrait.php b/core/modules/system/src/Tests/Menu/AssertMenuActiveTrailTrait.php new file mode 100644 index 0000000..a3146ba --- /dev/null +++ b/core/modules/system/src/Tests/Menu/AssertMenuActiveTrailTrait.php @@ -0,0 +1,68 @@ + $link_title) { + $part_xpath = (!$i ? '//' : '/following-sibling::ul/descendant::'); + $part_xpath .= 'li[contains(@class, :class)]/a[contains(@href, :href) and contains(text(), :title)]'; + $part_args = array( + ':class' => 'active-trail', + ':href' => _url($link_path), + ':title' => $link_title, + ); + $xpath .= $this->buildXPathQuery($part_xpath, $part_args); + $i++; + } + $elements = $this->xpath($xpath); + $this->assertTrue(!empty($elements), 'Active trail to current page was found in menu tree.'); + + // Append prefix for active link asserted below. + $xpath .= '/following-sibling::ul/descendant::'; + } + else { + $xpath .= '//'; + } + $xpath_last_active = ($last_active ? 'and contains(@class, :class-active)' : ''); + $xpath .= 'li[contains(@class, :class-trail)]/a[contains(@href, :href) ' . $xpath_last_active . 'and contains(text(), :title)]'; + $args = array( + ':class-trail' => 'active-trail', + ':class-active' => 'active', + ':href' => _url($active_link_path), + ':title' => $active_link_title, + ); + $elements = $this->xpath($xpath, $args); + $this->assertTrue(!empty($elements), format_string('Active link %title was found in menu tree, including active trail links %tree.', array( + '%title' => $active_link_title, + '%tree' => implode(' » ', $tree), + ))); + } + +} diff --git a/core/modules/system/src/Tests/Menu/MenuTestBase.php b/core/modules/system/src/Tests/Menu/MenuTestBase.php index 2478af3..833d72f 100644 --- a/core/modules/system/src/Tests/Menu/MenuTestBase.php +++ b/core/modules/system/src/Tests/Menu/MenuTestBase.php @@ -7,145 +7,10 @@ namespace Drupal\system\Tests\Menu; -use Drupal\Component\Utility\String; use Drupal\simpletest\WebTestBase; abstract class MenuTestBase extends WebTestBase { - /** - * Assert that a given path shows certain breadcrumb links. - * - * @param string $goto - * (optional) A system path to pass to - * Drupal\simpletest\WebTestBase::drupalGet(). - * @param array $trail - * An associative array whose keys are expected breadcrumb link paths and - * whose values are expected breadcrumb link texts (not sanitized). - * @param string $page_title - * (optional) A page title to additionally assert via - * Drupal\simpletest\WebTestBase::assertTitle(). Without site name suffix. - * @param array $tree - * (optional) An associative array whose keys are link paths and whose - * values are link titles (not sanitized) of an expected active trail in a - * menu tree output on the page. - * @param $last_active - * (optional) Whether the last link in $tree is expected to be active (TRUE) - * or just to be in the active trail (FALSE). - */ - protected function assertBreadcrumb($goto, array $trail, $page_title = NULL, array $tree = array(), $last_active = TRUE) { - if (isset($goto)) { - $this->drupalGet($goto); - } - $this->assertBreadcrumbParts($trail); + use AssertBreadcrumbTrait; - // Additionally assert page title, if given. - if (isset($page_title)) { - $this->assertTitle(strtr('@title | Drupal', array('@title' => $page_title))); - } - - // Additionally assert active trail in a menu tree output, if given. - if ($tree) { - $this->assertMenuActiveTrail($tree, $last_active); - } - } - - /** - * Assert that a trail exists in the internal browser. - * - * @param array $trail - * An associative array whose keys are expected breadcrumb link paths and - * whose values are expected breadcrumb link texts (not sanitized). - */ - protected function assertBreadcrumbParts($trail) { - // Compare paths with actual breadcrumb. - $parts = $this->getBreadcrumbParts(); - $pass = TRUE; - // There may be more than one breadcrumb on the page. If $trail is empty - // this test would go into an infinite loop, so we need to check that too. - while ($trail && !empty($parts)) { - foreach ($trail as $path => $title) { - $url = _url($path); - $part = array_shift($parts); - $pass = ($pass && $part['href'] === $url && $part['text'] === String::checkPlain($title)); - } - } - // No parts must be left, or an expected "Home" will always pass. - $pass = ($pass && empty($parts)); - - $this->assertTrue($pass, format_string('Breadcrumb %parts found on @path.', array( - '%parts' => implode(' » ', $trail), - '@path' => $this->getUrl(), - ))); - } - - /** - * Assert that active trail exists in a menu tree output. - * - * @param array $tree - * An associative array whose keys are link paths and whose - * values are link titles (not sanitized) of an expected active trail in a - * menu tree output on the page. - * @param bool $last_active - * Whether the last link in $tree is expected to be active (TRUE) - * or just to be in the active trail (FALSE). - */ - protected function assertMenuActiveTrail($tree, $last_active) { - end($tree); - $active_link_path = key($tree); - $active_link_title = array_pop($tree); - $xpath = ''; - if ($tree) { - $i = 0; - foreach ($tree as $link_path => $link_title) { - $part_xpath = (!$i ? '//' : '/following-sibling::ul/descendant::'); - $part_xpath .= 'li[contains(@class, :class)]/a[contains(@href, :href) and contains(text(), :title)]'; - $part_args = array( - ':class' => 'active-trail', - ':href' => _url($link_path), - ':title' => $link_title, - ); - $xpath .= $this->buildXPathQuery($part_xpath, $part_args); - $i++; - } - $elements = $this->xpath($xpath); - $this->assertTrue(!empty($elements), 'Active trail to current page was found in menu tree.'); - - // Append prefix for active link asserted below. - $xpath .= '/following-sibling::ul/descendant::'; - } - else { - $xpath .= '//'; - } - $xpath_last_active = ($last_active ? 'and contains(@class, :class-active)' : ''); - $xpath .= 'li[contains(@class, :class-trail)]/a[contains(@href, :href) ' . $xpath_last_active . 'and contains(text(), :title)]'; - $args = array( - ':class-trail' => 'active-trail', - ':class-active' => 'active', - ':href' => _url($active_link_path), - ':title' => $active_link_title, - ); - $elements = $this->xpath($xpath, $args); - $this->assertTrue(!empty($elements), format_string('Active link %title was found in menu tree, including active trail links %tree.', array( - '%title' => $active_link_title, - '%tree' => implode(' » ', $tree), - ))); - } - - /** - * Returns the breadcrumb contents of the current page in the internal browser. - */ - protected function getBreadcrumbParts() { - $parts = array(); - $elements = $this->xpath('//nav[@class="breadcrumb"]/ol/li/a'); - if (!empty($elements)) { - foreach ($elements as $element) { - $parts[] = array( - 'text' => (string) $element, - 'href' => (string) $element['href'], - 'title' => (string) $element['title'], - ); - } - } - return $parts; - } } diff --git a/core/modules/taxonomy/src/TermBreadcrumbBuilder.php b/core/modules/taxonomy/src/TermBreadcrumbBuilder.php index 8240b8f..58d9ef5 100644 --- a/core/modules/taxonomy/src/TermBreadcrumbBuilder.php +++ b/core/modules/taxonomy/src/TermBreadcrumbBuilder.php @@ -8,7 +8,7 @@ namespace Drupal\taxonomy; use Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface; -use Drupal\Core\Language\LanguageInterface; +use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Link; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\StringTranslation\StringTranslationTrait; @@ -20,6 +20,20 @@ class TermBreadcrumbBuilder implements BreadcrumbBuilderInterface { use StringTranslationTrait; /** + * The entity manager. + * + * @var \Drupal\Core\Entity\EntityManagerInterface + */ + protected $entityManager; + + /** + * Setup the entity manager. + */ + public function __construct(EntityManagerInterface $entityManager) { + $this->entityManager = $entityManager; + } + + /** * {@inheritdoc} */ public function applies(RouteMatchInterface $route_match) { @@ -36,10 +50,10 @@ public function build(RouteMatchInterface $route_match) { // hard-coded presumption. Make this behavior configurable per // vocabulary or term. $breadcrumb = array(); - $language = \Drupal::service('language_manager')->getCurrentLanguage(LanguageInterface::TYPE_CONTENT); - while ($parents = \Drupal::entityManager()->getStorage('taxonomy_term')->loadParents($term->id())) { + $term = $this->entityManager->getTranslationFromContext($term); + while ($parents = $this->entityManager->getStorage('taxonomy_term')->loadParents($term->id())) { $term = array_shift($parents); - $term = $term->getTranslation($language->getId()); + $term = $this->entityManager->getTranslationFromContext($term); $breadcrumb[] = Link::createFromRoute($term->getName(), 'entity.taxonomy_term.canonical', array('taxonomy_term' => $term->id())); } $breadcrumb[] = Link::createFromRoute($this->t('Home'), ''); diff --git a/core/modules/taxonomy/src/Tests/TaxonomyTranslationTestBase.php b/core/modules/taxonomy/src/Tests/TaxonomyTranslationTestBase.php deleted file mode 100644 index be5a145..0000000 --- a/core/modules/taxonomy/src/Tests/TaxonomyTranslationTestBase.php +++ /dev/null @@ -1,111 +0,0 @@ -translateToLangcode)->save(); - } - - /** - * Enables translations where it needed. - */ - protected function enableTranslation() { - // Enable translation for the current entity type and ensure the change is - // picked up. - \Drupal::service('content_translation.manager')->setEnabled('node', 'article', TRUE); - \Drupal::service('content_translation.manager')->setEnabled('taxonomy_term', $this->vocabulary->id(), TRUE); - drupal_static_reset(); - \Drupal::entityManager()->clearCachedDefinitions(); - \Drupal::service('router.builder')->rebuild(); - } - - /** - * Adds term reference field for the article content type. - */ - protected function setUpTermReferenceField() { - entity_create('field_storage_config', array( - 'field_name' => $this->termFieldName, - 'entity_type' => 'node', - 'type' => 'taxonomy_term_reference', - 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED, - 'translatable' => FALSE, - 'settings' => array( - 'allowed_values' => array( - array( - 'vocabulary' => $this->vocabulary->id(), - 'parent' => 0, - ), - ), - ), - ))->save(); - - $field = entity_create('field_config', array( - 'field_name' => $this->termFieldName, - 'bundle' => 'article', - 'entity_type' => 'node', - )); - $field->save(); - entity_get_form_display('node', 'article', 'default') - ->setComponent($this->termFieldName, array( - 'type' => 'taxonomy_autocomplete', - )) - ->save(); - entity_get_display('node', 'article', 'default') - ->setComponent($this->termFieldName, array( - 'type' => 'taxonomy_term_reference_link', - )) - ->save(); - } - -} diff --git a/core/modules/taxonomy/src/Tests/TaxonomyTranslationTestTrait.php b/core/modules/taxonomy/src/Tests/TaxonomyTranslationTestTrait.php new file mode 100644 index 0000000..e813ced --- /dev/null +++ b/core/modules/taxonomy/src/Tests/TaxonomyTranslationTestTrait.php @@ -0,0 +1,111 @@ +translateToLangcode)->save(); + } + + /** + * Enables translations where it needed. + */ + protected function enableTranslation() { + // Enable translation for the current entity type and ensure the change is + // picked up. + \Drupal::service('content_translation.manager')->setEnabled('node', 'article', TRUE); + \Drupal::service('content_translation.manager')->setEnabled('taxonomy_term', $this->vocabulary->id(), TRUE); + drupal_static_reset(); + \Drupal::entityManager()->clearCachedDefinitions(); + \Drupal::service('router.builder')->rebuild(); + } + + /** + * Adds term reference field for the article content type. + */ + protected function setUpTermReferenceField() { + entity_create('field_storage_config', array( + 'field_name' => $this->termFieldName, + 'entity_type' => 'node', + 'type' => 'taxonomy_term_reference', + 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED, + 'translatable' => FALSE, + 'settings' => array( + 'allowed_values' => array( + array( + 'vocabulary' => $this->vocabulary->id(), + 'parent' => 0, + ), + ), + ), + ))->save(); + + $field = entity_create('field_config', array( + 'field_name' => $this->termFieldName, + 'bundle' => 'article', + 'entity_type' => 'node', + )); + $field->save(); + entity_get_form_display('node', 'article', 'default') + ->setComponent($this->termFieldName, array( + 'type' => 'taxonomy_autocomplete', + )) + ->save(); + entity_get_display('node', 'article', 'default') + ->setComponent($this->termFieldName, array( + 'type' => 'taxonomy_term_reference_link', + )) + ->save(); + } + +} diff --git a/core/modules/taxonomy/src/Tests/TermTranslationBreadcrumbTest.php b/core/modules/taxonomy/src/Tests/TermTranslationBreadcrumbTest.php index a2f92e0..903e651 100644 --- a/core/modules/taxonomy/src/Tests/TermTranslationBreadcrumbTest.php +++ b/core/modules/taxonomy/src/Tests/TermTranslationBreadcrumbTest.php @@ -7,14 +7,17 @@ namespace Drupal\taxonomy\Tests; -use Drupal\Component\Utility\String; +use Drupal\system\Tests\Menu\AssertBreadcrumbTrait; /** * Tests for proper breadcrumb translation. * * @group taxonomy */ -class TermTranslationBreadcrumbTest extends TaxonomyTranslationTestBase { +class TermTranslationBreadcrumbTest extends TaxonomyTestBase { + + use AssertBreadcrumbTrait; + use TaxonomyTranslationTestTrait; /** * Term to translated term mapping. @@ -84,96 +87,6 @@ public function testTranslatedBreadcrumbs() { } /** - * Assert that a given path shows certain breadcrumb links. - * - * @param string $goto - * (optional) A system path to pass to - * Drupal\simpletest\WebTestBase::drupalGet(). - * @param array $trail - * An associative array whose keys are expected breadcrumb link paths and - * whose values are expected breadcrumb link texts (not sanitized). - * @param string $page_title - * (optional) A page title to additionally assert via - * Drupal\simpletest\WebTestBase::assertTitle(). Without site name suffix. - * @param array $tree - * (optional) An associative array whose keys are link paths and whose - * values are link titles (not sanitized) of an expected active trail in a - * menu tree output on the page. - * @param $last_active - * (optional) Whether the last link in $tree is expected to be active (TRUE) - * or just to be in the active trail (FALSE). - * - * @see \Drupal\system\Tests\Menu\MenuTestBase::assertBreadcrumb() - */ - protected function assertBreadcrumb($goto, array $trail, $page_title = NULL, array $tree = array(), $last_active = TRUE) { - if (isset($goto)) { - $this->drupalGet($goto); - } - $this->assertBreadcrumbParts($trail); - - // Additionally assert page title, if given. - if (isset($page_title)) { - $this->assertTitle(strtr('@title | Drupal', array('@title' => $page_title))); - } - - // Additionally assert active trail in a menu tree output, if given. - if ($tree) { - $this->assertMenuActiveTrail($tree, $last_active); - } - } - - /** - * Assert that a trail exists in the internal browser. - * - * @param array $trail - * An associative array whose keys are expected breadcrumb link paths and - * whose values are expected breadcrumb link texts (not sanitized). - * - * @see \Drupal\system\Tests\Menu\MenuTestBase::assertBreadcrumbParts() - */ - protected function assertBreadcrumbParts($trail) { - // Compare paths with actual breadcrumb. - $parts = $this->getBreadcrumbParts(); - - $pass = TRUE; - // There may be more than one breadcrumb on the page. If $trail is empty - // this test would go into an infinite loop, so we need to check that too. - while ($trail && !empty($parts)) { - foreach ($trail as $path => $title) { - $url = _url($path); - $part = array_shift($parts); - $pass = ($pass && $part['href'] === $url && $part['text'] === String::checkPlain($title)); - } - } - // No parts must be left, or an expected "Home" will always pass. - $pass = ($pass && empty($parts)); - $this->assertTrue($pass, format_string('Breadcrumb %parts found on @path.', array( - '%parts' => implode(' » ', $trail), - '@path' => $this->getUrl(), - ))); - } - - /** - * Returns the breadcrumb contents of the current page in the internal browser. - * - * @see \Drupal\system\Tests\Menu\MenuTestBase::getBreadcrumbParts() - */ - protected function getBreadcrumbParts() { - $parts = array(); - $elements = $this->xpath('//nav[@class="breadcrumb"]/ol/li/a'); - if (!empty($elements)) { - foreach ($elements as $element) { - $parts[] = array( - 'text' => (string) $element, - 'href' => (string) $element['href'], - 'title' => (string) $element['title'], - ); - } - } - return $parts; - } - - /** * Setup translated terms in a hierarchy. */ protected function setUpTerms() { diff --git a/core/modules/taxonomy/src/Tests/TermTranslationFieldViewTest.php b/core/modules/taxonomy/src/Tests/TermTranslationFieldViewTest.php index cd3c382..9474d11 100644 --- a/core/modules/taxonomy/src/Tests/TermTranslationFieldViewTest.php +++ b/core/modules/taxonomy/src/Tests/TermTranslationFieldViewTest.php @@ -12,7 +12,9 @@ * * @group taxonomy */ -class TermTranslationFieldViewTest extends TaxonomyTranslationTestBase { +class TermTranslationFieldViewTest extends TaxonomyTestBase { + + use TaxonomyTranslationTestTrait; /** * The term that should be translated. diff --git a/core/modules/taxonomy/taxonomy.services.yml b/core/modules/taxonomy/taxonomy.services.yml index 686f6ea..a8153ed 100644 --- a/core/modules/taxonomy/taxonomy.services.yml +++ b/core/modules/taxonomy/taxonomy.services.yml @@ -1,5 +1,6 @@ services: taxonomy_term.breadcrumb: class: Drupal\taxonomy\TermBreadcrumbBuilder + arguments: ['@entity.manager'] tags: - { name: breadcrumb_builder, priority: 1002 }