View source
<?php
namespace Drupal\taxonomy\Tests\Views;
use Drupal\Core\Language\LanguageInterface;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\views\Tests\ViewTestBase;
use Drupal\views\Tests\ViewTestData;
use Drupal\views\Views;
class TaxonomyFieldFilterTest extends ViewTestBase {
public static $modules = array(
'language',
'taxonomy',
'taxonomy_test_views',
'text',
'views',
'node',
);
public static $testViews = array(
'test_field_filters',
);
protected $vocabulary;
public $termNames = [];
function setUp() {
parent::setUp();
ConfigurableLanguage::createFromLangcode('fr')
->save();
ConfigurableLanguage::createFromLangcode('es')
->save();
$this->termNames = array(
'en' => 'Food in Paris',
'es' => 'Comida en Paris',
'fr' => 'Nouriture en Paris',
);
$this->vocabulary = entity_create('taxonomy_vocabulary', array(
'name' => 'Views testing tags',
'vid' => 'views_testing_tags',
));
$this->vocabulary
->save();
$field = entity_create('field_storage_config', array(
'field_name' => 'field_foo',
'entity_type' => 'taxonomy_term',
'type' => 'text',
));
$field
->save();
entity_create('field_config', array(
'field_name' => 'field_foo',
'entity_type' => 'taxonomy_term',
'label' => 'Foo',
'bundle' => 'views_testing_tags',
))
->save();
$taxonomy = $this
->createTermWithProperties(array(
'name' => $this->termNames['en'],
'langcode' => 'en',
'description' => $this->termNames['en'],
'field_foo' => $this->termNames['en'],
));
foreach (array(
'es',
'fr',
) as $langcode) {
$translation = $taxonomy
->addTranslation($langcode, array(
'name' => $this->termNames[$langcode],
));
$translation->description->value = $this->termNames[$langcode];
$translation->field_foo->value = $this->termNames[$langcode];
}
$taxonomy
->save();
Views::viewsData()
->clear();
ViewTestData::createTestViews(get_class($this), array(
'taxonomy_test_views',
));
$this->container
->get('router.builder')
->rebuild();
}
public function testFilters() {
$this
->assertPageCounts('test-name-filter', array(
'es' => 1,
'fr' => 0,
'en' => 0,
), 'Comida name filter');
$this
->assertPageCounts('test-desc-filter', array(
'es' => 1,
'fr' => 0,
'en' => 0,
), 'Comida description filter');
$this
->assertPageCounts('test-field-filter', array(
'es' => 1,
'fr' => 0,
'en' => 0,
), 'Comida field filter');
$this
->assertPageCounts('test-name-paris', array(
'es' => 1,
'fr' => 1,
'en' => 1,
), 'Paris name filter');
$this
->assertPageCounts('test-desc-paris', array(
'es' => 1,
'fr' => 1,
'en' => 1,
), 'Paris description filter');
$this
->assertPageCounts('test-field-paris', array(
'es' => 1,
'fr' => 1,
'en' => 1,
), 'Paris field filter');
}
protected function assertPageCounts($path, $counts, $message) {
$this
->drupalGet($path);
$text = $this
->getTextContent();
foreach ($counts as $langcode => $count) {
$this
->assertEqual(substr_count($text, $this->termNames[$langcode]), 2 * $count, 'Translation ' . $langcode . ' has count ' . $count . ' with ' . $message);
}
}
protected function createTermWithProperties($properties) {
$filter_formats = filter_formats();
$format = array_pop($filter_formats);
$properties += array(
'name' => $this
->randomMachineName(),
'description' => $this
->randomMachineName(),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
'field_foo' => $this
->randomMachineName(),
);
$term = entity_create('taxonomy_term', array(
'name' => $properties['name'],
'description' => $properties['description'],
'format' => $format
->id(),
'vid' => $this->vocabulary
->id(),
'langcode' => $properties['langcode'],
));
$term->field_foo->value = $properties['field_foo'];
$term
->save();
return $term;
}
}