Drupal 8.0.0 beta 12. More info: https://www.drupal.org/node/2514176

This commit is contained in:
Pantheon Automation 2015-08-17 17:00:26 -07:00 committed by Greg Anderson
commit 9921556621
13277 changed files with 1459781 additions and 0 deletions

View file

@ -0,0 +1,63 @@
<?php
/**
* @file
* Contains \Drupal\taxonomy\Tests\Views\RelationshipNodeTermDataTest.
*/
namespace Drupal\taxonomy\Tests\Views;
use Drupal\views\Views;
/**
* Tests the taxonomy term on node relationship handler.
*
* @group taxonomy
*/
class RelationshipNodeTermDataTest extends TaxonomyTestBase {
/**
* Views used by this test.
*
* @var array
*/
public static $testViews = array('test_taxonomy_node_term_data');
function testViewsHandlerRelationshipNodeTermData() {
$view = Views::getView('test_taxonomy_node_term_data');
// Tests \Drupal\taxonomy\Plugin\views\relationship\NodeTermData::calculateDependencies().
$expected = [
'config' => ['core.entity_view_mode.node.teaser'],
'module' => [
'node',
'taxonomy',
'user',
],
];
$this->assertIdentical($expected, $view->calculateDependencies());
$this->executeView($view, array($this->term1->id(), $this->term2->id()));
$expected_result = array(
array(
'nid' => $this->nodes[1]->id(),
),
array(
'nid' => $this->nodes[0]->id(),
),
);
$column_map = array('nid' => 'nid');
$this->assertIdenticalResultset($view, $expected_result, $column_map);
// Change the view to test relation limited by vocabulary.
$this->config('views.view.test_taxonomy_node_term_data')
->set('display.default.display_options.relationships.term_node_tid.vids', ['views_testing_tags'])
->save();
$view = Views::getView('test_taxonomy_node_term_data');
// Tests \Drupal\taxonomy\Plugin\views\relationship\NodeTermData::calculateDependencies().
$expected['config'][] = 'taxonomy.vocabulary.views_testing_tags';
$this->assertIdentical($expected, $view->calculateDependencies());
$this->executeView($view, array($this->term1->id(), $this->term2->id()));
$this->assertIdenticalResultset($view, $expected_result, $column_map);
}
}

View file

@ -0,0 +1,45 @@
<?php
/**
* @file
* Contains \Drupal\taxonomy\Tests\Views\RelationshipRepresentativeNodeTest.
*/
namespace Drupal\taxonomy\Tests\Views;
use Drupal\views\Views;
/**
* Tests the representative node relationship for terms.
*
* @group taxonomy
*/
class RelationshipRepresentativeNodeTest extends TaxonomyTestBase {
/**
* Views used by this test.
*
* @var array
*/
public static $testViews = array('test_groupwise_term');
/**
* Tests the relationship.
*/
public function testRelationship() {
$view = Views::getView('test_groupwise_term');
$this->executeView($view);
$map = array('node_field_data_taxonomy_term_field_data_nid' => 'nid', 'tid' => 'tid');
$expected_result = array(
array(
'nid' => $this->nodes[1]->id(),
'tid' => $this->term2->id(),
),
array(
'nid' => $this->nodes[1]->id(),
'tid' => $this->term1->id(),
),
);
$this->assertIdenticalResultset($view, $expected_result, $map);
}
}

View file

@ -0,0 +1,62 @@
<?php
/**
* @file
* Contains \Drupal\taxonomy\Tests\Views\TaxonomyDefaultArgumentTest.
*/
namespace Drupal\taxonomy\Tests\Views;
use Drupal\views\Views;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;
/**
* Tests the representative node relationship for terms.
*
* @group taxonomy
*/
class TaxonomyDefaultArgumentTest extends TaxonomyTestBase {
/**
* Views used by this test.
*
* @var array
*/
public static $testViews = array('taxonomy_default_argument_test');
/**
* Tests the relationship.
*/
public function testNodePath() {
$view = Views::getView('taxonomy_default_argument_test');
$request = Request::create($this->nodes[0]->url());
$request->server->set('SCRIPT_NAME', $GLOBALS['base_path'] . 'index.php');
$request->server->set('SCRIPT_FILENAME', 'index.php');
$response = $this->container->get('http_kernel')->handle($request, HttpKernelInterface::SUB_REQUEST);
$view->setRequest($request);
$view->setResponse($response);
$view->initHandlers();
$expected = implode(',', array($this->term1->id(), $this->term2->id()));
$this->assertEqual($expected, $view->argument['tid']->getDefaultArgument());
}
public function testTermPath() {
$view = Views::getView('taxonomy_default_argument_test');
$request = Request::create($this->term1->url());
$request->server->set('SCRIPT_NAME', $GLOBALS['base_path'] . 'index.php');
$request->server->set('SCRIPT_FILENAME', 'index.php');
$response = $this->container->get('http_kernel')->handle($request, HttpKernelInterface::SUB_REQUEST);
$view->setRequest($request);
$view->setResponse($response);
$view->initHandlers();
$expected = $this->term1->id();
$this->assertEqual($expected, $view->argument['tid']->getDefaultArgument());
}
}

View file

@ -0,0 +1,186 @@
<?php
/**
* @file
* Contains \Drupal\taxonomy\Tests\Views\TaxonomyFieldFilterTest.
*/
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;
/**
* Tests taxonomy field filters with translations.
*
* @group taxonomy
*/
class TaxonomyFieldFilterTest extends ViewTestBase {
/**
* {@inheritdoc}
*/
public static $modules = array('language', 'taxonomy', 'taxonomy_test_views', 'text', 'views', 'node');
/**
* Views used by this test.
*
* @var array
*/
public static $testViews = array('test_field_filters');
/**
* The vocabulary used for creating terms.
*
* @var \Drupal\taxonomy\VocabularyInterface
*/
protected $vocabulary;
/**
* List of taxonomy term names by language.
*
* @var array
*/
public $termNames = [];
function setUp() {
parent::setUp();
// Add two new languages.
ConfigurableLanguage::createFromLangcode('fr')->save();
ConfigurableLanguage::createFromLangcode('es')->save();
// Set up term names.
$this->termNames = array(
'en' => 'Food in Paris',
'es' => 'Comida en Paris',
'fr' => 'Nouriture en Paris',
);
// Create a vocabulary.
$this->vocabulary = entity_create('taxonomy_vocabulary', array(
'name' => 'Views testing tags',
'vid' => 'views_testing_tags',
));
$this->vocabulary->save();
// Add a translatable field to the vocabulary.
$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();
// Create term with translations.
$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();
}
/**
* Tests description and term name filters.
*/
public function testFilters() {
// Test the name filter page, which filters for name contains 'Comida'.
// Should show just the Spanish translation, once.
$this->assertPageCounts('test-name-filter', array('es' => 1, 'fr' => 0, 'en' => 0), 'Comida name filter');
// Test the description filter page, which filters for description contains
// 'Comida'. Should show just the Spanish translation, once.
$this->assertPageCounts('test-desc-filter', array('es' => 1, 'fr' => 0, 'en' => 0), 'Comida description filter');
// Test the field filter page, which filters for field_foo contains
// 'Comida'. Should show just the Spanish translation, once.
$this->assertPageCounts('test-field-filter', array('es' => 1, 'fr' => 0, 'en' => 0), 'Comida field filter');
// Test the name Paris filter page, which filters for name contains
// 'Paris'. Should show each translation once.
$this->assertPageCounts('test-name-paris', array('es' => 1, 'fr' => 1, 'en' => 1), 'Paris name filter');
// Test the description Paris page, which filters for description contains
// 'Paris'. Should show each translation, once.
$this->assertPageCounts('test-desc-paris', array('es' => 1, 'fr' => 1, 'en' => 1), 'Paris description filter');
// Test the field Paris filter page, which filters for field_foo contains
// 'Paris'. Should show each translation once.
$this->assertPageCounts('test-field-paris', array('es' => 1, 'fr' => 1, 'en' => 1), 'Paris field filter');
}
/**
* Asserts that the given taxonomy translation counts are correct.
*
* @param string $path
* Path of the page to test.
* @param array $counts
* Array whose keys are languages, and values are the number of times
* that translation should be shown on the given page.
* @param string $message
* Message suffix to display.
*/
protected function assertPageCounts($path, $counts, $message) {
// Get the text of the page.
$this->drupalGet($path);
$text = $this->getTextContent();
// Check the counts. Note that the title and body are both shown on the
// page, and they are the same. So the title/body string should appear on
// the page twice as many times as the input count.
foreach ($counts as $langcode => $count) {
$this->assertEqual(substr_count($text, $this->termNames[$langcode]), 2 * $count, 'Translation ' . $langcode . ' has count ' . $count . ' with ' . $message);
}
}
/**
* Creates a taxonomy term with specified name and other properties.
*
* @param array $properties
* Array of properties and field values to set.
*
* @return \Drupal\taxonomy\TermInterface
* The created taxonomy term.
*/
protected function createTermWithProperties($properties) {
// Use the first available text format.
$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;
}
}

View file

@ -0,0 +1,36 @@
<?php
/**
* @file
* Contains \Drupal\taxonomy\Tests\Views\TaxonomyFieldTidTest.
*/
namespace Drupal\taxonomy\Tests\Views;
use Drupal\views\Views;
/**
* Tests the taxonomy term TID field handler.
*
* @group taxonomy
*/
class TaxonomyFieldTidTest extends TaxonomyTestBase {
/**
* Views used by this test.
*
* @var array
*/
public static $testViews = array('test_taxonomy_tid_field');
function testViewsHandlerTidField() {
$view = Views::getView('test_taxonomy_tid_field');
$this->executeView($view);
$actual = $view->field['name']->advancedRender($view->result[0]);
$expected = \Drupal::l($this->term1->label(), $this->term1->urlInfo());
$this->assertEqual($expected, $actual);
}
}

View file

@ -0,0 +1,123 @@
<?php
/**
* @file
* Contains \Drupal\taxonomy\Tests\Views\TaxonomyIndexTidUiTest.
*/
namespace Drupal\taxonomy\Tests\Views;
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\views\Tests\ViewTestData;
use Drupal\views_ui\Tests\UITestBase;
/**
* Tests the taxonomy index filter handler UI.
*
* @group taxonomy
* @see \Drupal\taxonomy\Plugin\views\field\TaxonomyIndexTid
*/
class TaxonomyIndexTidUiTest extends UITestBase {
/**
* Views used by this test.
*
* @var array
*/
public static $testViews = array('test_filter_taxonomy_index_tid');
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('node', 'taxonomy', 'taxonomy_test_views');
/**
* A nested array of \Drupal\taxonomy\TermInterface objects.
*
* @var \Drupal\taxonomy\TermInterface[][]
*/
protected $terms = [];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
Vocabulary::create([
'vid' => 'tags',
'name' => 'Tags',
])->save();
// Setup a hierarchy which looks like this:
// term 0.0
// term 1.0
// - term 1.1
// term 2.0
// - term 2.1
// - term 2.2
for ($i = 0; $i < 3; $i++) {
for ($j = 0; $j <= $i; $j++) {
$this->terms[$i][$j] = $term = Term::create([
'vid' => 'tags',
'name' => "Term $i.$j",
'parent' => isset($terms[$i][0]) ? $terms[$i][0]->id() : 0,
]);
$term->save();
}
}
ViewTestData::createTestViews(get_class($this), array('taxonomy_test_views'));
}
/**
* Tests the filter UI.
*/
public function testFilterUI() {
$this->drupalGet('admin/structure/views/nojs/handler/test_filter_taxonomy_index_tid/default/filter/tid');
$result = $this->xpath('//select[@id="edit-options-value"]/option');
// Ensure that the expected hierarchy is available in the UI.
$counter = 0;
for ($i = 0; $i < 3; $i++) {
for ($j = 0; $j <= $i; $j++) {
$option = $result[$counter++];
$prefix = $this->terms[$i][$j]->parent->target_id ? '-' : '';
$attributes = $option->attributes();
$tid = (string) $attributes->value;
$this->assertEqual($prefix . $this->terms[$i][$j]->getName(), (string) $option);
$this->assertEqual($this->terms[$i][$j]->id(), $tid);
}
}
// Ensure the autocomplete input element appears when using the 'textfield'
// type.
$view = entity_load('view', 'test_filter_taxonomy_index_tid');
$display =& $view->getDisplay('default');
$display['display_options']['filters']['tid']['type'] = 'textfield';
$view->save();
$this->drupalGet('admin/structure/views/nojs/handler/test_filter_taxonomy_index_tid/default/filter/tid');
$this->assertFieldByXPath('//input[@id="edit-options-value"]');
// Tests \Drupal\taxonomy\Plugin\views\filter\TaxonomyIndexTid::calculateDependencies().
$expected = [
'config' => [
'taxonomy.vocabulary.tags',
],
'content' => [
'taxonomy_term:tags:' . Term::load(2)->uuid(),
],
'module' => [
'node',
'taxonomy',
'user',
],
];
$this->assertIdentical($expected, $view->calculateDependencies());
}
}

View file

@ -0,0 +1,52 @@
<?php
/**
* @file
* Contains \Drupal\taxonomy\Tests\Views\TaxonomyParentUITest.
*/
namespace Drupal\taxonomy\Tests\Views;
use Drupal\views\Tests\ViewTestData;
use Drupal\views_ui\Tests\UITestBase;
/**
* Tests views taxonomy parent plugin UI.
*
* @group taxonomy
* @see Drupal\taxonomy\Plugin\views\access\Role
*/
class TaxonomyParentUITest extends UITestBase {
/**
* Views used by this test.
*
* @var array
*/
public static $testViews = array('test_taxonomy_parent');
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('taxonomy', 'taxonomy_test_views');
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
ViewTestData::createTestViews(get_class($this), array('taxonomy_test_views'));
}
/**
* Tests the taxonomy parent plugin UI.
*/
public function testTaxonomyParentUI() {
$this->drupalGet('admin/structure/views/nojs/handler/test_taxonomy_parent/default/relationship/parent');
$this->assertNoText('The handler for this item is broken or missing.');
}
}

View file

@ -0,0 +1,111 @@
<?php
/**
* @file
* Contains \Drupal\taxonomy\Tests\Views\TaxonomyRelationshipTest.
*/
namespace Drupal\taxonomy\Tests\Views;
use Drupal\node\NodeInterface;
use Drupal\taxonomy\TermInterface;
use Drupal\views\Tests\ViewTestData;
use Drupal\views\Views;
/**
* Tests taxonomy relationships with parent term and node.
*
* @group taxonomy
*/
class TaxonomyRelationshipTest extends TaxonomyTestBase {
/**
* Stores the terms used in the tests.
*
* @var \Drupal\taxonomy\TermInterface[]
*/
protected $terms = array();
/**
* Views used by this test.
*
* @var array
*/
public static $testViews = array('test_taxonomy_term_relationship');
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
// Make term2 parent of term1.
$this->term1->set('parent', $this->term2->id());
$this->term1->save();
// Store terms in an array for testing.
$this->terms[] = $this->term1;
$this->terms[] = $this->term2;
// Only set term1 on node1 and term2 on node2 for testing.
unset($this->nodes[0]->field_views_testing_tags[1]);
$this->nodes[0]->save();
unset($this->nodes[1]->field_views_testing_tags[0]);
$this->nodes[1]->save();
Views::viewsData()->clear();
}
/**
* Tests the taxonomy parent plugin UI.
*/
public function testTaxonomyRelationships() {
// Check the generated views data of taxonomy_index.
$views_data = Views::viewsData()->get('taxonomy_index');
// Check the table join data.
$this->assertEqual($views_data['table']['join']['taxonomy_term_field_data']['left_field'], 'tid');
$this->assertEqual($views_data['table']['join']['taxonomy_term_field_data']['field'], 'tid');
$this->assertEqual($views_data['table']['join']['node_field_data']['left_field'], 'nid');
$this->assertEqual($views_data['table']['join']['node_field_data']['field'], 'nid');
$this->assertEqual($views_data['table']['join']['taxonomy_term_hierarchy']['left_field'], 'tid');
$this->assertEqual($views_data['table']['join']['taxonomy_term_hierarchy']['field'], 'tid');
// Check the generated views data of taxonomy_term_hierarchy.
$views_data = Views::viewsData()->get('taxonomy_term_hierarchy');
// Check the table join data.
$this->assertEqual($views_data['table']['join']['taxonomy_term_hierarchy']['left_field'], 'tid');
$this->assertEqual($views_data['table']['join']['taxonomy_term_hierarchy']['field'], 'parent');
$this->assertEqual($views_data['table']['join']['taxonomy_term_field_data']['left_field'], 'tid');
$this->assertEqual($views_data['table']['join']['taxonomy_term_field_data']['field'], 'tid');
// Check the parent relationship data.
$this->assertEqual($views_data['parent']['relationship']['base'], 'taxonomy_term_field_data');
$this->assertEqual($views_data['parent']['relationship']['field'], 'parent');
$this->assertEqual($views_data['parent']['relationship']['label'], t('Parent'));
$this->assertEqual($views_data['parent']['relationship']['id'], 'standard');
// Check the parent filter and argument data.
$this->assertEqual($views_data['parent']['filter']['id'], 'numeric');
$this->assertEqual($views_data['parent']['argument']['id'], 'taxonomy');
// Check an actual test view.
$view = Views::getView('test_taxonomy_term_relationship');
$this->executeView($view);
/** @var \Drupal\views\ResultRow $row */
foreach ($view->result as $index => $row) {
// Check that the actual ID of the entity is the expected one.
$this->assertEqual($row->tid, $this->terms[$index]->id());
// Also check that we have the correct result entity.
$this->assertEqual($row->_entity->id(), $this->terms[$index]->id());
$this->assertTrue($row->_entity instanceof TermInterface);
if (!$index) {
$this->assertTrue($row->_relationship_entities['parent'] instanceof TermInterface);
$this->assertEqual($row->_relationship_entities['parent']->id(), $this->term2->id());
$this->assertEqual($row->taxonomy_term_field_data_taxonomy_term_hierarchy_tid, $this->term2->id());
}
$this->assertTrue($row->_relationship_entities['nid'] instanceof NodeInterface);
$this->assertEqual($row->_relationship_entities['nid']->id(), $this->nodes[$index]->id());
}
}
}

View file

@ -0,0 +1,141 @@
<?php
/**
* @file
* Contains \Drupal\taxonomy\Tests\Views\TaxonomyTermFilterDepthTest.
*/
namespace Drupal\taxonomy\Tests\Views;
use Drupal\views\Views;
/**
* Test the taxonomy term with depth filter.
*
* @group taxonomy
*/
class TaxonomyTermFilterDepthTest extends TaxonomyTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['taxonomy', 'taxonomy_test_views', 'views', 'node'];
/**
* {@inheritdoc}
*/
public static $testViews = ['test_filter_taxonomy_index_tid_depth'];
/**
* @var \Drupal\taxonomy\TermInterface[]
*/
protected $terms = [];
/**
* @var \Drupal\views\ViewExecutable
*/
protected $view;
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
// Create a hierarchy 3 deep. Note the parent setup function creates two
// top-level terms w/o children.
$first = $this->createTerm(['name' => 'First']);
$second = $this->createTerm(['name' => 'Second', 'parent' => $first->id()]);
$third = $this->createTerm(['name' => 'Third', 'parent' => $second->id()]);
// Create a node w/o any terms.
$settings = ['type' => 'article'];
$this->nodes[] = $this->drupalCreateNode($settings);
// Create a node with only the top level term.
$settings['field_views_testing_tags'][0]['target_id'] = $first->id();
$this->nodes[] = $this->drupalCreateNode($settings);
// Create a node with only the third level term.
$settings['field_views_testing_tags'][0]['target_id'] = $third->id();
$this->nodes[] = $this->drupalCreateNode($settings);
$this->terms[0] = $first;
$this->terms[1] = $second;
$this->terms[2] = $third;
$this->view = Views::getView('test_filter_taxonomy_index_tid_depth');
}
/**
* Tests the terms with depth filter.
*/
public function testTermWithDepthFilter() {
$column_map = ['nid' => 'nid'];
$assert_method = 'assertIdentical';
// Default view has an empty value for this filter, so all nodes should be
// returned.
$expected = [
['nid' => 1],
['nid' => 2],
['nid' => 3],
['nid' => 4],
['nid' => 5],
];
$this->executeView($this->view);
$this->assertIdenticalResultsetHelper($this->view, $expected, $column_map, $assert_method);
// Set filter to search on top-level term, with depth 0.
$expected = [['nid' => 4]];
$this->assertTermWithDepthResult($this->terms[0]->id(), 0, $expected);
// Top-level term, depth 1.
$expected = [['nid' => 4]];
$this->assertTermWithDepthResult($this->terms[0]->id(), 0, $expected);
// Top-level term, depth 2.
$expected = [['nid' => 4], ['nid' => 5]];
$this->assertTermWithDepthResult($this->terms[0]->id(), 2, $expected);
// Second-level term, depth 1.
$expected = [['nid' => 5]];
$this->assertTermWithDepthResult($this->terms[1]->id(), 1, $expected);
// Third-level term, depth 0.
$expected = [['nid' => 5]];
$this->assertTermWithDepthResult($this->terms[2]->id(), 0, $expected);
// Third-level term, depth 1.
$expected = [['nid' => 5]];
$this->assertTermWithDepthResult($this->terms[2]->id(), 1, $expected);
// Third-level term, depth -2.
$expected = [['nid' => 4], ['nid' => 5]];
$this->assertTermWithDepthResult($this->terms[2]->id(), -2, $expected);
}
/**
* Changes the tid filter to given term and depth.
*
* @param integer $tid
* The term ID to filter on.
* @param integer $depth
* The depth to search.
* @param array $expected
* The expected views result.
*/
protected function assertTermWithDepthResult($tid, $depth, array $expected) {
$this->view->destroy();
$this->view->initDisplay();
$filters = $this->view->displayHandlers->get('default')
->getOption('filters');
$filters['tid_depth']['depth'] = $depth;
$filters['tid_depth']['value'] = [$tid];
$this->view->displayHandlers->get('default')
->setOption('filters', $filters);
$this->executeView($this->view);
$this->assertIdenticalResultsetHelper($this->view, $expected, ['nid' => 'nid'], 'assertIdentical');
}
}

View file

@ -0,0 +1,161 @@
<?php
/**
* @file
* Contains \Drupal\taxonomy\Tests\Views\TaxonomyTermViewTest.
*/
namespace Drupal\taxonomy\Tests\Views;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface;
use Drupal\views\Views;
/**
* Tests the taxonomy term view page and its translation.
*
* @group taxonomy
*/
class TaxonomyTermViewTest extends TaxonomyTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('taxonomy', 'views');
/**
* An user with permissions to administer taxonomy.
*
* @var \Drupal\user\UserInterface
*/
protected $adminUser;
/**
* Name of the taxonomy term reference field.
*
* @var string
*/
protected $fieldName1;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
// Create an administrative user.
$this->adminUser = $this->drupalCreateUser(['administer taxonomy', 'bypass node access']);
$this->drupalLogin($this->adminUser);
// Create a vocabulary and add two term reference fields to article nodes.
$this->fieldName1 = Unicode::strtolower($this->randomMachineName());
$handler_settings = array(
'target_bundles' => array(
$this->vocabulary->id() => $this->vocabulary->id(),
),
'auto_create' => TRUE,
);
$this->createEntityReferenceField('node', 'article', $this->fieldName1, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
entity_get_form_display('node', 'article', 'default')
->setComponent($this->fieldName1, array(
'type' => 'options_select',
))
->save();
entity_get_display('node', 'article', 'default')
->setComponent($this->fieldName1, array(
'type' => 'entity_reference_label',
))
->save();
}
/**
* Tests that the taxonomy term view is working properly.
*/
public function testTaxonomyTermView() {
// Create terms in the vocabulary.
$term = $this->createTerm();
// Post an article.
$edit = array();
$edit['title[0][value]'] = $original_title = $this->randomMachineName();
$edit['body[0][value]'] = $this->randomMachineName();
$edit["{$this->fieldName1}[]"] = $term->id();
$this->drupalPostForm('node/add/article', $edit, t('Save'));
$node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
$this->drupalGet('taxonomy/term/' . $term->id());
$this->assertText($term->label());
$this->assertText($node->label());
\Drupal::service('module_installer')->install(array('language', 'content_translation'));
$language = ConfigurableLanguage::createFromLangcode('ur');
$language->save();
// Enable translation for the article content type and ensure the change is
// picked up.
\Drupal::service('content_translation.manager')->setEnabled('node', 'article', TRUE);
$roles = $this->adminUser->getRoles(TRUE);
Role::load(reset($roles))
->grantPermission('create content translations')
->grantPermission('translate any entity')
->save();
drupal_static_reset();
\Drupal::entityManager()->clearCachedDefinitions();
\Drupal::service('router.builder')->rebuild();
\Drupal::service('entity.definition_update_manager')->applyUpdates();
$edit['title[0][value]'] = $translated_title = $this->randomMachineName();
$this->drupalPostForm('node/' . $node->id() . '/translations/add/en/ur', $edit, t('Save (this translation)'));
$this->drupalGet('taxonomy/term/' . $term->id());
$this->assertText($term->label());
$this->assertText($original_title);
$this->assertNoText($translated_title);
$this->drupalGet('ur/taxonomy/term/' . $term->id());
$this->assertText($term->label());
$this->assertNoText($original_title);
$this->assertText($translated_title);
// Uninstall language module and ensure that the language is not part of the
// query anymore.
// @see \Drupal\views\Plugin\views\filter\LanguageFilter::query()
$node->delete();
\Drupal::service('module_installer')->uninstall(['content_translation', 'language']);
$view = Views::getView('taxonomy_term');
$view->initDisplay();
$view->setArguments([$term->id()]);
$view->build();
/** @var \Drupal\Core\Database\Query\Select $query */
$query = $view->build_info['query'];
$tables = $query->getTables();
// Ensure that the join to node_field_data is not added by default.
$this->assertEqual(['node_field_data', 'taxonomy_index'], array_keys($tables));
// Ensure that the filter to the language column is not there by default.
$condition = $query->conditions();
// We only want to check the no. of conditions in the query.
unset($condition['#conjunction']);
$this->assertEqual(1, count($condition));
// Clear permissions for anonymous users to check access for default views.
Role::load(RoleInterface::ANONYMOUS_ID)->revokePermission('access content')->save();
// Test the default views disclose no data by default.
$this->drupalLogout();
$this->drupalGet('taxonomy/term/' . $term->id());
$this->assertResponse(403);
$this->drupalGet('taxonomy/term/' . $term->id() . '/feed');
$this->assertResponse(403);
}
}

View file

@ -0,0 +1,146 @@
<?php
/**
* @file
* Contains \Drupal\taxonomy\Tests\Views\TaxonomyTestBase.
*/
namespace Drupal\taxonomy\Tests\Views;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\entity_reference\Tests\EntityReferenceTestTrait;
use Drupal\views\Tests\ViewTestBase;
use Drupal\views\Tests\ViewTestData;
/**
* Base class for all taxonomy tests.
*/
abstract class TaxonomyTestBase extends ViewTestBase {
use EntityReferenceTestTrait;
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('taxonomy', 'taxonomy_test_views');
/**
* Stores the nodes used for the different tests.
*
* @var \Drupal\node\NodeInterface[]
*/
protected $nodes = array();
/**
* The vocabulary used for creating terms.
*
* @var \Drupal\taxonomy\VocabularyInterface
*/
protected $vocabulary;
/**
* Stores the first term used in the different tests.
*
* @var \Drupal\taxonomy\TermInterface
*/
protected $term1;
/**
* Stores the second term used in the different tests.
*
* @var \Drupal\taxonomy\TermInterface
*/
protected $term2;
protected function setUp() {
parent::setUp();
$this->mockStandardInstall();
ViewTestData::createTestViews(get_class($this), array('taxonomy_test_views'));
$this->term1 = $this->createTerm();
$this->term2 = $this->createTerm();
$node = array();
$node['type'] = 'article';
$node['field_views_testing_tags'][]['target_id'] = $this->term1->id();
$node['field_views_testing_tags'][]['target_id'] = $this->term2->id();
$this->nodes[] = $this->drupalCreateNode($node);
$this->nodes[] = $this->drupalCreateNode($node);
}
/**
* Provides a workaround for the inability to use the standard profile.
*
* @see https://www.drupal.org/node/1708692
*/
protected function mockStandardInstall() {
$this->drupalCreateContentType(array(
'type' => 'article',
));
// Create the vocabulary for the tag field.
$this->vocabulary = entity_create('taxonomy_vocabulary', array(
'name' => 'Views testing tags',
'vid' => 'views_testing_tags',
));
$this->vocabulary->save();
$field_name = 'field_' . $this->vocabulary->id();
$handler_settings = array(
'target_bundles' => array(
$this->vocabulary->id() => $this->vocabulary->id(),
),
'auto_create' => TRUE,
);
$this->createEntityReferenceField('node', 'article', $field_name, 'Tags', 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
entity_get_form_display('node', 'article', 'default')
->setComponent($field_name, array(
'type' => 'entity_reference_autocomplete_tags',
'weight' => -4,
))
->save();
entity_get_display('node', 'article', 'default')
->setComponent($field_name, array(
'type' => 'entity_reference_label',
'weight' => 10,
))
->save();
entity_get_display('node', 'article', 'teaser')
->setComponent($field_name, array(
'type' => 'entity_reference_label',
'weight' => 10,
))
->save();
}
/**
* Returns a new term with random properties in vocabulary $vid.
*
* @param array $settings
* (Optional) An associative array of settings to pass to `entity_create`.
*
* @return \Drupal\taxonomy\Entity\Term
* The created taxonomy term.
*/
protected function createTerm(array $settings = []) {
$filter_formats = filter_formats();
$format = array_pop($filter_formats);
$settings += [
'name' => $this->randomMachineName(),
'description' => $this->randomMachineName(),
// Use the first available text format.
'format' => $format->id(),
'vid' => $this->vocabulary->id(),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
];
$term = entity_create('taxonomy_term', $settings);
$term->save();
return $term;
}
}

View file

@ -0,0 +1,74 @@
<?php
/**
* @file
* Contains \Drupal\taxonomy\Tests\Views\TaxonomyViewsFieldAccessTest.
*/
namespace Drupal\taxonomy\Tests\Views;
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\user\Entity\User;
use Drupal\views\Tests\Handler\FieldFieldAccessTestBase;
/**
* Tests base field access in Views for the taxonomy entity.
*
* @group taxonomy
*/
class TaxonomyViewsFieldAccessTest extends FieldFieldAccessTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['taxonomy', 'text', 'entity_test'];
/**
* {@inheritdoc}
*/
protected function setUp($import_test_views = TRUE) {
parent::setUp($import_test_views);
$this->installEntitySchema('taxonomy_term');
}
/**
* Check access for taxonomy fields.
*/
public function testTermFields() {
$vocab = Vocabulary::create([
'vid' => 'random',
'name' => 'Randomness',
]);
$vocab->save();
$term1 = Term::create([
'name' => 'Semi random',
'vid' => $vocab->id(),
]);
$term1->save();
$term2 = Term::create([
'name' => 'Majorly random',
'vid' => $vocab->id(),
]);
$term2->save();
$term3 = Term::create([
'name' => 'Not really random',
'vid' => $vocab->id(),
]);
$term3->save();
$this->assertFieldAccess('taxonomy_term', 'name', 'Majorly random');
$this->assertFieldAccess('taxonomy_term', 'name', 'Semi random');
$this->assertFieldAccess('taxonomy_term', 'name', 'Not really random');
$this->assertFieldAccess('taxonomy_term', 'tid', $term1->id());
$this->assertFieldAccess('taxonomy_term', 'tid', $term2->id());
$this->assertFieldAccess('taxonomy_term', 'tid', $term3->id());
$this->assertFieldAccess('taxonomy_term', 'uuid', $term1->uuid());
$this->assertFieldAccess('taxonomy_term', 'uuid', $term2->uuid());
$this->assertFieldAccess('taxonomy_term', 'uuid', $term3->uuid());
}
}

View file

@ -0,0 +1,53 @@
<?php
/**
* @file
* Contains \Drupal\taxonomy\Tests\Views\TermNameFieldTest.
*/
namespace Drupal\taxonomy\Tests\Views;
use Drupal\views\Views;
/**
* Tests the term_name field handler.
*
* @group taxonomy
*
* @see \Drupal\taxonomy\Plugin\views\field\TermName
*/
class TermNameFieldTest extends TaxonomyTestBase {
/**
* {@inheritdoc}
*/
public static $testViews = array('test_taxonomy_term_name');
/**
* Tests term name field plugin functionality.
*/
public function testTermNameField() {
$this->term1->name->value = $this->randomMachineName() . ' ' . $this->randomMachineName();
$this->term1->save();
$user = $this->drupalCreateUser(['access content']);
$this->drupalLogin($user);
$view = Views::getView('test_taxonomy_term_name');
$view->initDisplay();
$this->executeView($view);
$this->assertEqual($this->term1->getName(), $view->getStyle()->getField(0, 'name'));
$this->assertEqual($this->term2->getName(), $view->getStyle()->getField(1, 'name'));
$view = Views::getView('test_taxonomy_term_name');
$display =& $view->storage->getDisplay('default');
$display['display_options']['fields']['name']['convert_spaces'] = TRUE;
$view->storage->invalidateCaches();
$this->executeView($view);
$this->assertEqual(str_replace(' ', '-', $this->term1->getName()), $view->getStyle()->getField(0, 'name'));
$this->assertEqual($this->term2->getName(), $view->getStyle()->getField(1, 'name'));
}
}