entityreference.taxonomy.test 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. }