/** * Tests that default and custom attributes are handled correctly on links. */ function testLinkAttributes() { /** @var \Drupal\Core\Render\RendererInterface $renderer */ $renderer = $this->container->get('renderer'); // Test that hreflang is added when a link has a known language. $language = new Language(array('id' => 'fr', 'name' => 'French')); $hreflang_link = array('#type' => 'link', '#options' => array('language' => $language), '#url' => Url::fromUri('https://www.drupal.org'), '#title' => 'bar'); $langcode = $language->getId(); // Test that the default hreflang handling for links does not override a // hreflang attribute explicitly set in the render array. $hreflang_override_link = $hreflang_link; $hreflang_override_link['#options']['attributes']['hreflang'] = 'foo'; $rendered = $renderer->renderRoot($hreflang_link); $this->assertTrue($this->hasAttribute('hreflang', $rendered, $langcode), format_string('hreflang attribute with value @langcode is present on a rendered link when langcode is provided in the render array.', array('@langcode' => $langcode))); $rendered = $renderer->renderRoot($hreflang_override_link); $this->assertTrue($this->hasAttribute('hreflang', $rendered, 'foo'), format_string('hreflang attribute with value @hreflang is present on a rendered link when @hreflang is provided in the render array.', array('@hreflang' => 'foo'))); // Test the active class in links produced by // \Drupal\Core\Utility\LinkGeneratorInterface::generate() and #type 'link'. $options_no_query = array(); $options_query = array('query' => array('foo' => 'bar', 'one' => 'two')); $options_query_reverse = array('query' => array('one' => 'two', 'foo' => 'bar')); // Test #type link. $path = 'common-test/type-link-active-class'; $this->drupalGet($path, $options_no_query); $links = $this->xpath('//a[@href = :href and contains(@class, :class)]', array(':href' => Url::fromRoute('common_test.l_active_class', [], $options_no_query)->toString(), ':class' => 'is-active')); $this->assertTrue(isset($links[0]), 'A link generated by the link generator to the current page is marked active.'); $links = $this->xpath('//a[@href = :href and not(contains(@class, :class))]', array(':href' => Url::fromRoute('common_test.l_active_class', [], $options_query)->toString(), ':class' => 'is-active')); $this->assertTrue(isset($links[0]), 'A link generated by the link generator to the current page with a query string when the current page has no query string is not marked active.'); $this->drupalGet($path, $options_query); $links = $this->xpath('//a[@href = :href and contains(@class, :class)]', array(':href' => Url::fromRoute('common_test.l_active_class', [], $options_query)->toString(), ':class' => 'is-active')); $this->assertTrue(isset($links[0]), 'A link generated by the link generator to the current page with a query string that matches the current query string is marked active.'); $links = $this->xpath('//a[@href = :href and contains(@class, :class)]', array(':href' => Url::fromRoute('common_test.l_active_class', [], $options_query_reverse)->toString(), ':class' => 'is-active')); $this->assertTrue(isset($links[0]), 'A link generated by the link generator to the current page with a query string that has matching parameters to the current query string but in a different order is marked active.'); $links = $this->xpath('//a[@href = :href and not(contains(@class, :class))]', array(':href' => Url::fromRoute('common_test.l_active_class', [], $options_no_query)->toString(), ':class' => 'is-active')); $this->assertTrue(isset($links[0]), 'A link generated by the link generator to the current page without a query string when the current page has a query string is not marked active.'); // Test adding a custom class in links produced by // \Drupal\Core\Utility\LinkGeneratorInterface::generate() and #type 'link'. // Test the link generator. $class_l = $this->randomMachineName(); $link_l = \Drupal::l($this->randomMachineName(), new Url('<current>', [], ['attributes' => ['class' => [$class_l]]])); $this->assertTrue($this->hasAttribute('class', $link_l, $class_l), format_string('Custom class @class is present on link when requested by l()', array('@class' => $class_l))); // Test #type. $class_theme = $this->randomMachineName(); $type_link = array('#type' => 'link', '#title' => $this->randomMachineName(), '#url' => Url::fromRoute('<current>'), '#options' => array('attributes' => array('class' => array($class_theme)))); $link_theme = $renderer->renderRoot($type_link); $this->assertTrue($this->hasAttribute('class', $link_theme, $class_theme), format_string('Custom class @class is present on link when requested by #type', array('@class' => $class_theme))); }
/** * Provides data for testSortArrayOfLanguages. * * @return array * An array of test data. */ public function providerTestSortArrayOfLanguages() { $language9A = new Language(); $language9A->setName('A'); $language9A->setWeight(9); $language9A->setId('dd'); $language10A = new Language(); $language10A->setName('A'); $language10A->setWeight(10); $language10A->setId('ee'); $language10B = new Language(); $language10B->setName('B'); $language10B->setWeight(10); $language10B->setId('ff'); return array(array(array($language9A->getId() => $language9A, $language10B->getId() => $language10B), array($language9A->getId(), $language10B->getId())), array(array($language10B->getId() => $language10B, $language9A->getId() => $language9A), array($language9A->getId(), $language10B->getId())), array(array($language10A->getId() => $language10A, $language10B->getId() => $language10B), array($language10A->getId(), $language10B->getId())), array(array($language10B->getId() => $language10B, $language10A->getId() => $language10A), array($language10A->getId(), $language10B->getId()))); }
/** * Tests the getAliasByPath cache when a different language is requested. * * @covers ::getAliasByPath * @covers ::writeCache */ public function testGetAliasByPathCachedMissLanguage() { $path_part1 = $this->randomMachineName(); $path_part2 = $this->randomMachineName(); $path = $path_part1 . '/' . $path_part2; $alias = $this->randomMachineName(); $language = $this->setUpCurrentLanguage(); $cached_language = new Language(array('id' => 'de')); $cached_paths = array($cached_language->getId() => array($path)); $this->cache->expects($this->once())->method('get')->with($this->cacheKey)->will($this->returnValue((object) array('data' => $cached_paths))); // Simulate a request so that the preloaded paths are fetched. $this->aliasManager->setCacheKey($this->path); $this->aliasWhitelist->expects($this->any())->method('get')->with($path_part1)->will($this->returnValue(TRUE)); // The requested language is different than the cached, so this will // need to load. $this->aliasStorage->expects($this->never())->method('preloadPathAlias'); $this->aliasStorage->expects($this->once())->method('lookupPathAlias')->with($path, $language->getId())->will($this->returnValue($alias)); $this->assertEquals($alias, $this->aliasManager->getAliasByPath($path)); // Call it twice to test the static cache. $this->assertEquals($alias, $this->aliasManager->getAliasByPath($path)); // This needs to write out the cache. $expected_new_cache = array($cached_language->getId() => array($path), $language->getId() => array($path)); $this->cache->expects($this->once())->method('set')->with($this->cacheKey, $expected_new_cache, (int) $_SERVER['REQUEST_TIME'] + 60 * 60 * 24); $this->aliasManager->writeCache(); }
/** * Tests langcode ID getter and setter methods. * * @covers ::getId() * @covers ::setId() */ public function testGetLangcode() { $language_code = $this->randomName(2); $this->assertSame($this->language, $this->language->setId($language_code)); $this->assertSame($language_code, $this->language->getId()); }
/** * Provides data for testSortArrayOfLanguages. * * @return array * An array of test data. */ public function providerTestSortArrayOfLanguages() { $language9A = new Language(array('id' => 'dd', 'name' => 'A', 'weight' => 9)); $language10A = new Language(array('id' => 'ee', 'name' => 'A', 'weight' => 10)); $language10B = new Language(array('id' => 'ff', 'name' => 'B', 'weight' => 10)); return array(array(array($language9A->getId() => $language9A, $language10B->getId() => $language10B), array($language9A->getId(), $language10B->getId())), array(array($language10B->getId() => $language10B, $language9A->getId() => $language9A), array($language9A->getId(), $language10B->getId())), array(array($language10A->getId() => $language10A, $language10B->getId() => $language10B), array($language10A->getId(), $language10B->getId())), array(array($language10B->getId() => $language10B, $language10A->getId() => $language10A), array($language10A->getId(), $language10B->getId()))); }
/** * Tests ConfigNamesMapper::hasTranslation(). * * @param array $mock_return_values * An array of values that the mocked configuration mapper manager should * return for hasTranslation(). * @param bool $expected * The expected return value of ConfigNamesMapper::hasTranslation(). * * @dataProvider providerTestHasTranslation */ public function testHasTranslation(array $mock_return_values, $expected) { $language = new Language(); // As the configuration names are arbitrary, simply use integers. $config_names = range(1, count($mock_return_values)); $this->configNamesMapper->setConfigNames($config_names); $map = array(); foreach ($config_names as $i => $config_name) { $map[] = array($config_name, $language->getId(), $mock_return_values[$i]); } $this->localeConfigManager->expects($this->any())->method('hasTranslation')->will($this->returnValueMap($map)); $result = $this->configNamesMapper->hasTranslation($language); $this->assertSame($expected, $result); }