Projet

Général

Profil

Paste
Télécharger (4,85 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / entityreference / tests / entityreference.taxonomy.test @ 59ae487e

1
<?php
2

    
3
/**
4
 * Test for Entity Reference taxonomy integration.
5
 */
6
class EntityReferenceTaxonomyTestCase extends DrupalWebTestCase {
7
  public static function getInfo() {
8
    return array(
9
      'name' => 'Entity Reference Taxonomy',
10
      'description' => 'Tests nodes with reference to terms as indexed.',
11
      'group' => 'Entity Reference',
12
    );
13
  }
14

    
15
  public function setUp() {
16
    parent::setUp('entityreference', 'taxonomy');
17

    
18
    // Create an entity reference field.
19
    $field = array(
20
      'entity_types' => array('node'),
21
      'settings' => array(
22
        'handler' => 'base',
23
        'target_type' => 'taxonomy_term',
24
        'handler_settings' => array(
25
          'target_bundles' => array(),
26
        ),
27
      ),
28
      'field_name' => 'field_entityreference_term',
29
      'type' => 'entityreference',
30
    );
31
    $field = field_create_field($field);
32
    $instance = array(
33
      'field_name' => 'field_entityreference_term',
34
      'bundle' => 'article',
35
      'entity_type' => 'node',
36
    );
37

    
38
    // Enable the taxonomy-index behavior.
39
    $instance['settings']['behaviors']['taxonomy-index']['status'] = TRUE;
40
    field_create_instance($instance);
41

    
42
    // Create a term reference field.
43
    $field = array(
44
      'translatable' => FALSE,
45
      'entity_types' => array('node'),
46
      'settings' => array(
47
        'allowed_values' => array(
48
          array(
49
            'vocabulary' => 'terms',
50
            'parent' => 0,
51
          ),
52
        ),
53
      ),
54
      'field_name' => 'field_taxonomy_term',
55
      'type' => 'taxonomy_term_reference',
56
    );
57
    $field = field_create_field($field);
58
    $instance = array(
59
      'field_name' => 'field_taxonomy_term',
60
      'bundle' => 'article',
61
      'entity_type' => 'node',
62
    );
63
    field_create_instance($instance);
64

    
65
    // Create a terms vocobulary.
66
    $vocabulary = new stdClass();
67
    $vocabulary->name = 'Terms';
68
    $vocabulary->machine_name = 'terms';
69
    taxonomy_vocabulary_save($vocabulary);
70

    
71
    // Create term.
72
    for ($i = 1; $i <= 2; $i++) {
73
      $term = new stdClass();
74
      $term->name = "term $i";
75
      $term->vid = 1;
76
      taxonomy_term_save($term);
77
    }
78
  }
79

    
80
  /**
81
   * Test referencing a term using entity reference field.
82
   */
83
  public function testNodeIndex() {
84
    // Asert node insert with reference to term.
85
    $settings = array();
86
    $settings['type'] = 'article';
87
    $settings['field_entityreference_term'][LANGUAGE_NONE][0]['target_id'] = 1;
88
    $node = $this->drupalCreateNode($settings);
89

    
90
    $this->assertEqual(taxonomy_select_nodes(1), array($node->nid));
91

    
92
    // Asert node update with reference to term.
93
    node_save($node);
94
    $this->assertEqual(taxonomy_select_nodes(1), array($node->nid));
95

    
96
    // Assert node update with reference to term and taxonomy reference to
97
    // another term.
98
    $wrapper = entity_metadata_wrapper('node', $node);
99
    $wrapper->field_taxonomy_term->set(2);
100
    $wrapper->save();
101

    
102
    $this->assertEqual(taxonomy_select_nodes(1), array($node->nid));
103
    $this->assertEqual(taxonomy_select_nodes(2), array($node->nid));
104

    
105
    // Assert node update with reference to term and taxonomy reference to
106
    // same term.
107
    $wrapper->field_taxonomy_term->set(1);
108
    $wrapper->save();
109
    $this->assertEqual(taxonomy_select_nodes(1), array($node->nid));
110

    
111
    $wrapper->delete();
112
    $this->assertFalse(taxonomy_select_nodes(1));
113
  }
114

    
115
  /**
116
   * Add a second ER field from node/article to taxonomy.
117
   *
118
   * This should not cause {taxonomy_index} to receive duplicate entries.
119
   */
120
  protected function setupForIndexDuplicates() {
121
    // Create an entity reference field.
122
    $field = array(
123
      'entity_types' => array('node'),
124
      'settings' => array(
125
        'handler' => 'base',
126
        'target_type' => 'taxonomy_term',
127
        'handler_settings' => array(
128
          'target_bundles' => array(),
129
        ),
130
      ),
131
      'field_name' => 'field_entityreference_term2',
132
      'type' => 'entityreference',
133
    );
134
    $field = field_create_field($field);
135
    $instance = array(
136
      'field_name' => 'field_entityreference_term2',
137
      'bundle' => 'article',
138
      'entity_type' => 'node',
139
    );
140

    
141
    // Enable the taxonomy-index behavior.
142
    $instance['settings']['behaviors']['taxonomy-index']['status'] = TRUE;
143
    field_create_instance($instance);
144
  }
145

    
146
  /**
147
   * Make sure the index only contains one entry for a given node->term
148
   * reference, even when multiple ER fields link from the node bundle to terms.
149
   */
150
  public function testIndexDuplicates() {
151
    // Extra setup for this test: add another ER field on this content type.
152
    $this->setupForIndexDuplicates();
153

    
154
    // Assert node insert with reference to term in first field.
155
    $tid = 1;
156
    $settings = array();
157
    $settings['type'] = 'article';
158
    $settings['field_entityreference_term'][LANGUAGE_NONE][0]['target_id'] = $tid;
159
    $node = $this->drupalCreateNode($settings);
160

    
161
    $this->assertEqual(taxonomy_select_nodes($tid), array($node->nid));
162
  }
163
}