TermFieldMultipleVocabularyTest.php 4.73 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<?php

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

namespace Drupal\taxonomy\Tests;

/**
 * Tests a taxonomy term reference field that allows multiple vocabularies.
 */
class TermFieldMultipleVocabularyTest extends TaxonomyTestBase {

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

22 23 24 25 26 27 28 29 30 31 32 33 34
  protected $instance;
  protected $vocabulary1;
  protected $vocabulary2;

  public static function getInfo() {
    return array(
      'name' => 'Multiple vocabulary term reference field',
      'description' => 'Tests term reference fields that allow multiple vocabularies.',
      'group' => 'Taxonomy',
    );
  }

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

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

    // Set up a field and instance.
    $this->field_name = drupal_strtolower($this->randomName());
    $this->field = array(
      'field_name' => $this->field_name,
      'type' => 'taxonomy_term_reference',
      'cardinality' => FIELD_CARDINALITY_UNLIMITED,
      'settings' => array(
        'allowed_values' => array(
          array(
51
            'vocabulary' => $this->vocabulary1->id(),
52 53 54
            'parent' => '0',
          ),
          array(
55
            'vocabulary' => $this->vocabulary2->id(),
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
            '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);
71 72 73 74 75
    entity_get_display('test_entity', 'test_bundle', 'full')
      ->setComponent($this->field_name, array(
        'type' => 'taxonomy_term_reference_link',
      ))
      ->save();
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
  }

  /**
   * Tests term reference field and widget with multiple vocabularies.
   */
  function testTaxonomyTermFieldMultipleVocabularies() {
    // Create a term in each vocabulary.
    $term1 = $this->createTerm($this->vocabulary1);
    $term2 = $this->createTerm($this->vocabulary2);

    // Submit an entity with both terms.
    $langcode = LANGUAGE_NOT_SPECIFIED;
    $this->drupalGet('test-entity/add/test_bundle');
    $this->assertFieldByName("{$this->field_name}[$langcode][]", '', 'Widget is displayed');
    $edit = array(
91
      "{$this->field_name}[$langcode][]" => array($term1->id(), $term2->id()),
92 93 94 95 96 97 98 99 100
    );
    $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.');

    // Render the entity.
    $entity = field_test_entity_test_load($id);
    $entities = array($id => $entity);
101 102
    $display = entity_get_display($entity->entityType(), $entity->bundle(), 'full');
    field_attach_prepare_view('test_entity', $entities, array($entity->bundle() => $display));
103
    $entity->content = field_attach_view($entity, $display);
104
    $this->content = drupal_render($entity->content);
105 106
    $this->assertText($term1->label(), 'Term 1 name is displayed.');
    $this->assertText($term2->label(), 'Term 2 name is displayed.');
107 108

    // Delete vocabulary 2.
109
    $this->vocabulary2->delete();
110 111 112 113

    // Re-render the content.
    $entity = field_test_entity_test_load($id);
    $entities = array($id => $entity);
114 115
    $display = entity_get_display($entity->entityType(), $entity->bundle(), 'full');
    field_attach_prepare_view('test_entity', $entities, array($entity->bundle() => $display));
116
    $entity->content = field_attach_view($entity, $display);
117 118 119 120
    $this->plainTextContent = FALSE;
    $this->content = drupal_render($entity->content);

    // Term 1 should still be displayed; term 2 should not be.
121 122
    $this->assertText($term1->label(), 'Term 1 name is displayed.');
    $this->assertNoText($term2->label(), 'Term 2 name is not displayed.');
123 124 125

    // Verify that field and instance settings are correct.
    $field_info = field_info_field($this->field_name);
126
    $this->assertEqual(count($field_info['settings']['allowed_values']), 1, 'Only one vocabulary is allowed for the field.');
127 128 129 130 131 132 133

    // The widget should still be displayed.
    $this->drupalGet('test-entity/add/test_bundle');
    $this->assertFieldByName("{$this->field_name}[$langcode][]", '', 'Widget is still displayed');

    // Term 1 should still pass validation.
    $edit = array(
134
      "{$this->field_name}[$langcode][]" => array($term1->id()),
135 136 137 138
    );
    $this->drupalPost(NULL, $edit, t('Save'));
  }
}