entityreference.taxonomy.test 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. * Test for Entity Reference taxonomy integration.
  4. */
  5. class EntityReferenceTaxonomyTestCase extends DrupalWebTestCase {
  6. public static function getInfo() {
  7. return array(
  8. 'name' => 'Entity Reference Taxonomy',
  9. 'description' => 'Tests nodes with reference to terms as indexed.',
  10. 'group' => 'Entity Reference',
  11. );
  12. }
  13. public function setUp() {
  14. parent::setUp('entityreference', 'taxonomy');
  15. // Create an entity reference field.
  16. $field = array(
  17. 'entity_types' => array('node'),
  18. 'settings' => array(
  19. 'handler' => 'base',
  20. 'target_type' => 'taxonomy_term',
  21. 'handler_settings' => array(
  22. 'target_bundles' => array(),
  23. ),
  24. ),
  25. 'field_name' => 'field_entityreference_term',
  26. 'type' => 'entityreference',
  27. );
  28. $field = field_create_field($field);
  29. $instance = array(
  30. 'field_name' => 'field_entityreference_term',
  31. 'bundle' => 'article',
  32. 'entity_type' => 'node',
  33. );
  34. // Enable the taxonomy-index behavior.
  35. $instance['settings']['behaviors']['taxonomy-index']['status'] = TRUE;
  36. field_create_instance($instance);
  37. // Create a term reference field.
  38. $field = array(
  39. 'translatable' => FALSE,
  40. 'entity_types' => array('node'),
  41. 'settings' => array(
  42. 'allowed_values' => array(
  43. array(
  44. 'vocabulary' => 'terms',
  45. 'parent' => 0,
  46. ),
  47. ),
  48. ),
  49. 'field_name' => 'field_taxonomy_term',
  50. 'type' => 'taxonomy_term_reference',
  51. );
  52. $field = field_create_field($field);
  53. $instance = array(
  54. 'field_name' => 'field_taxonomy_term',
  55. 'bundle' => 'article',
  56. 'entity_type' => 'node',
  57. );
  58. field_create_instance($instance);
  59. // Create a terms vocobulary.
  60. $vocabulary = new stdClass();
  61. $vocabulary->name = 'Terms';
  62. $vocabulary->machine_name = 'terms';
  63. taxonomy_vocabulary_save($vocabulary);
  64. // Create term.
  65. for ($i = 1; $i <= 2; $i++) {
  66. $term = new stdClass();
  67. $term->name = "term $i";
  68. $term->vid = 1;
  69. taxonomy_term_save($term);
  70. }
  71. }
  72. /**
  73. * Test referencing a term using entity reference field.
  74. */
  75. public function testNodeIndex() {
  76. // Asert node insert with reference to term.
  77. $settings = array();
  78. $settings['type'] = 'article';
  79. $settings['field_entityreference_term'][LANGUAGE_NONE][0]['target_id'] = 1;
  80. $node = $this->drupalCreateNode($settings);
  81. $this->assertEqual(taxonomy_select_nodes(1), array($node->nid));
  82. // Asert node update with reference to term.
  83. node_save($node);
  84. $this->assertEqual(taxonomy_select_nodes(1), array($node->nid));
  85. // Assert node update with reference to term and taxonomy reference to
  86. // another term.
  87. $wrapper = entity_metadata_wrapper('node', $node);
  88. $wrapper->field_taxonomy_term->set(2);
  89. $wrapper->save();
  90. $this->assertEqual(taxonomy_select_nodes(1), array($node->nid));
  91. $this->assertEqual(taxonomy_select_nodes(2), array($node->nid));
  92. // Assert node update with reference to term and taxonomy reference to
  93. // same term.
  94. $wrapper->field_taxonomy_term->set(1);
  95. $wrapper->save();
  96. $this->assertEqual(taxonomy_select_nodes(1), array($node->nid));
  97. $wrapper->delete();
  98. $this->assertFalse(taxonomy_select_nodes(1));
  99. }
  100. /**
  101. * Add a second ER field from node/article to taxonomy.
  102. *
  103. * This should not cause {taxonomy_index} to receive duplicate entries.
  104. */
  105. protected function setupForIndexDuplicates() {
  106. // Create an entity reference field.
  107. $field = array(
  108. 'entity_types' => array('node'),
  109. 'settings' => array(
  110. 'handler' => 'base',
  111. 'target_type' => 'taxonomy_term',
  112. 'handler_settings' => array(
  113. 'target_bundles' => array(),
  114. ),
  115. ),
  116. 'field_name' => 'field_entityreference_term2',
  117. 'type' => 'entityreference',
  118. );
  119. $field = field_create_field($field);
  120. $instance = array(
  121. 'field_name' => 'field_entityreference_term2',
  122. 'bundle' => 'article',
  123. 'entity_type' => 'node',
  124. );
  125. // Enable the taxonomy-index behavior.
  126. $instance['settings']['behaviors']['taxonomy-index']['status'] = TRUE;
  127. field_create_instance($instance);
  128. }
  129. /**
  130. * Make sure the index only contains one entry for a given node->term
  131. * reference, even when multiple ER fields link from the node bundle to terms.
  132. */
  133. public function testIndexDuplicates() {
  134. // Extra setup for this test: add another ER field on this content type.
  135. $this->setupForIndexDuplicates();
  136. // Assert node insert with reference to term in first field.
  137. $tid = 1;
  138. $settings = array();
  139. $settings['type'] = 'article';
  140. $settings['field_entityreference_term'][LANGUAGE_NONE][0]['target_id'] = $tid;
  141. $node = $this->drupalCreateNode($settings);
  142. $this->assertEqual(taxonomy_select_nodes($tid), array($node->nid));
  143. }
  144. }