summaryrefslogtreecommitdiffstats
path: root/core/modules/taxonomy/src/Tests/TermFieldTest.php
blob: 50d23e8e40607afb884a37251439778e6550d3e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php

/**
 * @file
 * Definition of Drupal\taxonomy\Tests\TermFieldTest.
 */

namespace Drupal\taxonomy\Tests;

use Drupal\Component\Utility\Unicode;
use Drupal\field\Entity\FieldStorageConfig;

/**
 * Tests the creation of term fields.
 *
 * @group taxonomy
 */
class TermFieldTest extends TaxonomyTestBase {

  /**
   * The field storage to test against.
   *
   * @var \Drupal\field\FieldStorageConfigInterface
   */
  protected $field_storage;

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array('entity_test', 'field_ui');

  protected $vocabulary;

  protected function setUp() {
    parent::setUp();

    $web_user = $this->drupalCreateUser(array(
      'view test entity',
      'administer entity_test content',
      'administer taxonomy',
      'administer entity_test fields',
    ));
    $this->drupalLogin($web_user);
    $this->vocabulary = $this->createVocabulary();

    // Setup a field.
    $this->field_name = Unicode::strtolower($this->randomMachineName());
    $this->field_storage = entity_create('field_storage_config', array(
      'field_name' => $this->field_name,
      'entity_type' => 'entity_test',
      'type' => 'taxonomy_term_reference',
      'settings' => array(
        'allowed_values' => array(
          array(
            'vocabulary' => $this->vocabulary->id(),
            'parent' => '0',
          ),
        ),
      )
    ));
    $this->field_storage->save();
    entity_create('field_config', array(
      'field_storage' => $this->field_storage,
      'bundle' => 'entity_test',
    ))->save();
    entity_get_form_display('entity_test', 'entity_test', 'default')
      ->setComponent($this->field_name, array(
        'type' => 'options_select',
      ))
      ->save();
    entity_get_display('entity_test', 'entity_test', 'full')
      ->setComponent($this->field_name, array(
        'type' => 'taxonomy_term_reference_link',
      ))
      ->save();
  }

  /**
   * Test term field validation.
   */
  function testTaxonomyTermFieldValidation() {
    // Test validation with a valid value.
    $term = $this->createTerm($this->vocabulary);
    $entity = entity_create('entity_test');
    $entity->{$this->field_name}->target_id = $term->id();
    $violations = $entity->{$this->field_name}->validate();
    $this->assertEqual(count($violations) , 0, 'Correct term does not cause validation error.');

    // Test validation with an invalid valid value (wrong vocabulary).
    $bad_term = $this->createTerm($this->createVocabulary());
    $entity = entity_create('entity_test');
    $entity->{$this->field_name}->target_id = $bad_term->id();
    $violations = $entity->{$this->field_name}->validate();
    $this->assertEqual(count($violations) , 1, 'Wrong term causes validation error.');
  }

  /**
   * Test widgets.
   */
  function testTaxonomyTermFieldWidgets() {
    // Create a term in the vocabulary.
    $term = $this->createTerm($this->vocabulary);

    // Display creation form.
    $this->drupalGet('entity_test/add');
    $this->assertFieldByName($this->field_name, NULL, 'Widget is displayed.');

    // Submit with some value.
    $edit = array(
      $this->field_name => array($term->id()),
    );
    $this->drupalPostForm(NULL, $edit, t('Save'));
    preg_match('|entity_test/manage/(\d+)|', $this->url, $match);
    $id = $match[1];
    $this->assertText(t('entity_test @id has been created.', array('@id' => $id)));

    // Display the object.
    $entity = entity_load('entity_test', $id);
    $display = entity_get_display($entity->getEntityTypeId(), $entity->bundle(), 'full');
    $content = $display->build($entity);
    $this->drupalSetContent(drupal_render($content));
    $this->assertText($term->getName(), 'Term label is displayed.');

    // Delete the vocabulary and verify that the widget is gone.
    $this->vocabulary->delete();
    $this->drupalGet('entity_test/add');
    $this->assertNoFieldByName($this->field_name, '', 'Widget is not displayed.');
  }

  /**
   * No php error message on the field setting page for autocomplete widget.
   */
  function testTaxonomyTermFieldSettingsAutocompleteWidget() {
    entity_get_form_display('entity_test', 'entity_test', 'default')
      ->setComponent($this->field_name, array(
        'type' => 'taxonomy_autocomplete',
        'weight' => 1,
      ))
      ->save();
    $this->drupalGet('entity_test/structure/entity_test/fields/entity_test.entity_test.' . $this->field_name);
    $this->assertNoErrorsLogged();
  }

  /**
   * Tests that vocabulary machine name changes are mirrored in field definitions.
   */
  function testTaxonomyTermFieldChangeMachineName() {
    // Add several entries in the 'allowed_values' setting, to make sure that
    // they all get updated.
    $this->field_storage->settings['allowed_values'] = array(
      array(
        'vocabulary' => $this->vocabulary->id(),
        'parent' => '0',
      ),
      array(
        'vocabulary' => $this->vocabulary->id(),
        'parent' => '0',
      ),
      array(
        'vocabulary' => 'foo',
        'parent' => '0',
      ),
    );
    $this->field_storage->save();
    // Change the machine name.
    $new_name = Unicode::strtolower($this->randomMachineName());
    $this->vocabulary->vid = $new_name;
    $this->vocabulary->save();

    // Check that the field is still attached to the vocabulary.
    $field_storage = FieldStorageConfig::loadByName('entity_test', $this->field_name);
    $allowed_values = $field_storage->getSetting('allowed_values');
    $this->assertEqual($allowed_values[0]['vocabulary'], $new_name, 'Index 0: Machine name was updated correctly.');
    $this->assertEqual($allowed_values[1]['vocabulary'], $new_name, 'Index 1: Machine name was updated correctly.');
    $this->assertEqual($allowed_values[2]['vocabulary'], 'foo', 'Index 2: Machine name was left untouched.');
  }
}