PageRenderTime 24ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/taxonomy/src/Tests/TermIndexTest.php

http://github.com/drupal/drupal
PHP | 214 lines | 132 code | 31 blank | 51 comment | 0 complexity | fd7c13e92a65cb2dfdc4a03319c7d74e MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. namespace Drupal\taxonomy\Tests;
  3. use Drupal\Component\Utility\Unicode;
  4. use Drupal\Core\Field\FieldStorageDefinitionInterface;
  5. /**
  6. * Tests the hook implementations that maintain the taxonomy index.
  7. *
  8. * @group taxonomy
  9. */
  10. class TermIndexTest extends TaxonomyTestBase {
  11. /**
  12. * Modules to enable.
  13. *
  14. * @var array
  15. */
  16. public static $modules = array('views');
  17. /**
  18. * Vocabulary for testing.
  19. *
  20. * @var \Drupal\taxonomy\VocabularyInterface
  21. */
  22. protected $vocabulary;
  23. /**
  24. * Name of the taxonomy term reference field.
  25. *
  26. * @var string
  27. */
  28. protected $fieldName1;
  29. /**
  30. * Name of the taxonomy term reference field.
  31. *
  32. * @var string
  33. */
  34. protected $fieldName2;
  35. protected function setUp() {
  36. parent::setUp();
  37. // Create an administrative user.
  38. $this->drupalLogin($this->drupalCreateUser(['administer taxonomy', 'bypass node access']));
  39. // Create a vocabulary and add two term reference fields to article nodes.
  40. $this->vocabulary = $this->createVocabulary();
  41. $this->fieldName1 = Unicode::strtolower($this->randomMachineName());
  42. $handler_settings = array(
  43. 'target_bundles' => array(
  44. $this->vocabulary->id() => $this->vocabulary->id(),
  45. ),
  46. 'auto_create' => TRUE,
  47. );
  48. $this->createEntityReferenceField('node', 'article', $this->fieldName1, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
  49. entity_get_form_display('node', 'article', 'default')
  50. ->setComponent($this->fieldName1, array(
  51. 'type' => 'options_select',
  52. ))
  53. ->save();
  54. entity_get_display('node', 'article', 'default')
  55. ->setComponent($this->fieldName1, array(
  56. 'type' => 'entity_reference_label',
  57. ))
  58. ->save();
  59. $this->fieldName2 = Unicode::strtolower($this->randomMachineName());
  60. $this->createEntityReferenceField('node', 'article', $this->fieldName2, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
  61. entity_get_form_display('node', 'article', 'default')
  62. ->setComponent($this->fieldName2, array(
  63. 'type' => 'options_select',
  64. ))
  65. ->save();
  66. entity_get_display('node', 'article', 'default')
  67. ->setComponent($this->fieldName2, array(
  68. 'type' => 'entity_reference_label',
  69. ))
  70. ->save();
  71. }
  72. /**
  73. * Tests that the taxonomy index is maintained properly.
  74. */
  75. function testTaxonomyIndex() {
  76. $node_storage = $this->container->get('entity.manager')->getStorage('node');
  77. // Create terms in the vocabulary.
  78. $term_1 = $this->createTerm($this->vocabulary);
  79. $term_2 = $this->createTerm($this->vocabulary);
  80. // Post an article.
  81. $edit = array();
  82. $edit['title[0][value]'] = $this->randomMachineName();
  83. $edit['body[0][value]'] = $this->randomMachineName();
  84. $edit["{$this->fieldName1}[]"] = $term_1->id();
  85. $edit["{$this->fieldName2}[]"] = $term_1->id();
  86. $this->drupalPostForm('node/add/article', $edit, t('Save'));
  87. // Check that the term is indexed, and only once.
  88. $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
  89. $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array(
  90. ':nid' => $node->id(),
  91. ':tid' => $term_1->id(),
  92. ))->fetchField();
  93. $this->assertEqual(1, $index_count, 'Term 1 is indexed once.');
  94. // Update the article to change one term.
  95. $edit["{$this->fieldName1}[]"] = $term_2->id();
  96. $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
  97. // Check that both terms are indexed.
  98. $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array(
  99. ':nid' => $node->id(),
  100. ':tid' => $term_1->id(),
  101. ))->fetchField();
  102. $this->assertEqual(1, $index_count, 'Term 1 is indexed.');
  103. $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array(
  104. ':nid' => $node->id(),
  105. ':tid' => $term_2->id(),
  106. ))->fetchField();
  107. $this->assertEqual(1, $index_count, 'Term 2 is indexed.');
  108. // Update the article to change another term.
  109. $edit["{$this->fieldName2}[]"] = $term_2->id();
  110. $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
  111. // Check that only one term is indexed.
  112. $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array(
  113. ':nid' => $node->id(),
  114. ':tid' => $term_1->id(),
  115. ))->fetchField();
  116. $this->assertEqual(0, $index_count, 'Term 1 is not indexed.');
  117. $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array(
  118. ':nid' => $node->id(),
  119. ':tid' => $term_2->id(),
  120. ))->fetchField();
  121. $this->assertEqual(1, $index_count, 'Term 2 is indexed once.');
  122. // Redo the above tests without interface.
  123. $node_storage->resetCache(array($node->id()));
  124. $node = $node_storage->load($node->id());
  125. $node->title = $this->randomMachineName();
  126. // Update the article with no term changed.
  127. $node->save();
  128. // Check that the index was not changed.
  129. $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array(
  130. ':nid' => $node->id(),
  131. ':tid' => $term_1->id(),
  132. ))->fetchField();
  133. $this->assertEqual(0, $index_count, 'Term 1 is not indexed.');
  134. $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array(
  135. ':nid' => $node->id(),
  136. ':tid' => $term_2->id(),
  137. ))->fetchField();
  138. $this->assertEqual(1, $index_count, 'Term 2 is indexed once.');
  139. // Update the article to change one term.
  140. $node->{$this->fieldName1} = array(array('target_id' => $term_1->id()));
  141. $node->save();
  142. // Check that both terms are indexed.
  143. $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array(
  144. ':nid' => $node->id(),
  145. ':tid' => $term_1->id(),
  146. ))->fetchField();
  147. $this->assertEqual(1, $index_count, 'Term 1 is indexed.');
  148. $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array(
  149. ':nid' => $node->id(),
  150. ':tid' => $term_2->id(),
  151. ))->fetchField();
  152. $this->assertEqual(1, $index_count, 'Term 2 is indexed.');
  153. // Update the article to change another term.
  154. $node->{$this->fieldName2} = array(array('target_id' => $term_1->id()));
  155. $node->save();
  156. // Check that only one term is indexed.
  157. $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array(
  158. ':nid' => $node->id(),
  159. ':tid' => $term_1->id(),
  160. ))->fetchField();
  161. $this->assertEqual(1, $index_count, 'Term 1 is indexed once.');
  162. $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array(
  163. ':nid' => $node->id(),
  164. ':tid' => $term_2->id(),
  165. ))->fetchField();
  166. $this->assertEqual(0, $index_count, 'Term 2 is not indexed.');
  167. }
  168. /**
  169. * Tests that there is a link to the parent term on the child term page.
  170. */
  171. function testTaxonomyTermHierarchyBreadcrumbs() {
  172. // Create two taxonomy terms and set term2 as the parent of term1.
  173. $term1 = $this->createTerm($this->vocabulary);
  174. $term2 = $this->createTerm($this->vocabulary);
  175. $term1->parent = array($term2->id());
  176. $term1->save();
  177. // Verify that the page breadcrumbs include a link to the parent term.
  178. $this->drupalGet('taxonomy/term/' . $term1->id());
  179. // Breadcrumbs are not rendered with a language, prevent the term
  180. // language from being added to the options.
  181. $this->assertRaw(\Drupal::l($term2->getName(), $term2->urlInfo('canonical', ['language' => NULL])), 'Parent term link is displayed when viewing the node.');
  182. }
  183. }