TermFieldTest.php 5.45 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<?php

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

namespace Drupal\taxonomy\Tests;

use Drupal\field\FieldValidationException;

/**
 * Tests for taxonomy term field and formatter.
 */
class TermFieldTest extends TaxonomyTestBase {

17 18 19 20 21 22 23
  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array('field_test');

24 25 26 27 28 29 30 31 32 33 34 35
  protected $instance;
  protected $vocabulary;

  public static function getInfo() {
    return array(
      'name' => 'Taxonomy term reference field',
      'description' => 'Test the creation of term fields.',
      'group' => 'Taxonomy',
    );
  }

  function setUp() {
36
    parent::setUp();
37 38 39 40 41 42 43 44 45 46 47 48 49

    $web_user = $this->drupalCreateUser(array('access field_test content', 'administer field_test content', 'administer taxonomy'));
    $this->drupalLogin($web_user);
    $this->vocabulary = $this->createVocabulary();

    // Setup a field and instance.
    $this->field_name = drupal_strtolower($this->randomName());
    $this->field = array(
      'field_name' => $this->field_name,
      'type' => 'taxonomy_term_reference',
      'settings' => array(
        'allowed_values' => array(
          array(
50
            'vocabulary' => $this->vocabulary->id(),
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
            'parent' => '0',
          ),
        ),
      )
    );
    field_create_field($this->field);
    $this->instance = array(
      'field_name' => $this->field_name,
      'entity_type' => 'test_entity',
      'bundle' => 'test_bundle',
      'widget' => array(
        'type' => 'options_select',
      ),
    );
    field_create_instance($this->instance);
66 67 68 69 70
    entity_get_display('test_entity', 'test_bundle', 'full')
      ->setComponent($this->field_name, array(
        'type' => 'taxonomy_term_reference_link',
      ))
      ->save();
71 72 73 74 75 76 77 78
  }

  /**
   * Test term field validation.
   */
  function testTaxonomyTermFieldValidation() {
    // Test valid and invalid values with field_attach_validate().
    $langcode = LANGUAGE_NOT_SPECIFIED;
79
    $entity = field_test_create_entity();
80
    $term = $this->createTerm($this->vocabulary);
81
    $entity->{$this->field_name}[$langcode][0]['tid'] = $term->id();
82
    try {
83
      field_attach_validate($entity);
84 85 86 87 88 89
      $this->pass('Correct term does not cause validation error.');
    }
    catch (FieldValidationException $e) {
      $this->fail('Correct term does not cause validation error.');
    }

90
    $entity = field_test_create_entity();
91
    $bad_term = $this->createTerm($this->createVocabulary());
92
    $entity->{$this->field_name}[$langcode][0]['tid'] = $bad_term->id();
93
    try {
94
      field_attach_validate($entity);
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
      $this->fail('Wrong term causes validation error.');
    }
    catch (FieldValidationException $e) {
      $this->pass('Wrong term causes validation error.');
    }
  }

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

    // Display creation form.
    $langcode = LANGUAGE_NOT_SPECIFIED;
    $this->drupalGet('test-entity/add/test_bundle');
    $this->assertFieldByName("{$this->field_name}[$langcode]", '', 'Widget is displayed.');

    // Submit with some value.
    $edit = array(
116
      "{$this->field_name}[$langcode]" => array($term->id()),
117 118 119 120 121 122 123 124 125
    );
    $this->drupalPost(NULL, $edit, t('Save'));
    preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match);
    $id = $match[1];
    $this->assertRaw(t('test_entity @id has been created.', array('@id' => $id)), 'Entity was created.');

    // Display the object.
    $entity = field_test_entity_test_load($id);
    $entities = array($id => $entity);
126 127
    $display = entity_get_display($entity->entityType(), $entity->bundle(), 'full');
    field_attach_prepare_view('test_entity', $entities, array($entity->bundle() => $display));
128
    $entity->content = field_attach_view($entity, $display);
129
    $this->content = drupal_render($entity->content);
130
    $this->assertText($term->label(), 'Term label is displayed.');
131 132

    // Delete the vocabulary and verify that the widget is gone.
133
    $this->vocabulary->delete();
134 135 136 137 138 139 140 141 142 143 144 145
    $this->drupalGet('test-entity/add/test_bundle');
    $this->assertNoFieldByName("{$this->field_name}[$langcode]", '', 'Widget is not displayed');
  }

  /**
   * 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['settings']['allowed_values'] = array(
      array(
146
        'vocabulary' => $this->vocabulary->id(),
147 148 149
        'parent' => '0',
      ),
      array(
150
        'vocabulary' => $this->vocabulary->id(),
151 152 153 154 155 156 157 158 159 160
        'parent' => '0',
      ),
      array(
        'vocabulary' => 'foo',
        'parent' => '0',
      ),
    );
    field_update_field($this->field);
    // Change the machine name.
    $new_name = drupal_strtolower($this->randomName());
161
    $this->vocabulary->vid = $new_name;
162
    $this->vocabulary->save();
163 164 165 166 167 168 169 170 171

    // Check that the field instance is still attached to the vocabulary.
    $field = field_info_field($this->field_name);
    $allowed_values = $field['settings']['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.');
  }
}