Newer
Older
Angie Byron
committed
<?php
/**
* @file
* Contains \Drupal\taxonomy\Tests\TaxonomyTermReferenceItemTest.
*/
namespace Drupal\taxonomy\Tests;
use Drupal\Component\Utility\Unicode;
Alex Pott
committed
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Language\LanguageInterface;
Angie Byron
committed
use Drupal\field\Tests\FieldUnitTestBase;
use Drupal\taxonomy\Entity\Term;
Angie Byron
committed
/**
* Tests the new entity API for the taxonomy term reference field type.
*
* @group taxonomy
Angie Byron
committed
*/
Angie Byron
committed
class TaxonomyTermReferenceItemTest extends FieldUnitTestBase {
Angie Byron
committed
/**
* Modules to enable.
*
* @var array
*/
Alex Pott
committed
public static $modules = array('taxonomy', 'entity_reference', 'text', 'filter');
Angie Byron
committed
Alex Pott
committed
/**
* The term entity.
*
* @var \Drupal\taxonomy\TermInterface
*/
protected $term;
Alex Pott
committed
protected function setUp() {
Angie Byron
committed
parent::setUp();
$this->installEntitySchema('taxonomy_term');
Angie Byron
committed
Angie Byron
committed
$vocabulary = entity_create('taxonomy_vocabulary', array(
Alex Pott
committed
'name' => $this->randomMachineName(),
'vid' => Unicode::strtolower($this->randomMachineName()),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
Angie Byron
committed
));
$vocabulary->save();
entity_create('field_storage_config', array(
Alex Pott
committed
'field_name' => 'field_test_taxonomy',
'entity_type' => 'entity_test',
Angie Byron
committed
'type' => 'taxonomy_term_reference',
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
Angie Byron
committed
'settings' => array(
'allowed_values' => array(
array(
'vocabulary' => $vocabulary->id(),
Angie Byron
committed
'parent' => 0,
),
),
),
))->save();
entity_create('field_config', array(
Angie Byron
committed
'entity_type' => 'entity_test',
'field_name' => 'field_test_taxonomy',
'bundle' => 'entity_test',
))->save();
Angie Byron
committed
$this->term = entity_create('taxonomy_term', array(
Alex Pott
committed
'name' => $this->randomMachineName(),
'vid' => $vocabulary->id(),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
Angie Byron
committed
));
$this->term->save();
}
/**
* Tests using entity fields of the taxonomy term reference field type.
*/
public function testTaxonomyTermReferenceItem() {
$tid = $this->term->id();
// Just being able to create the entity like this verifies a lot of code.
$entity = entity_create('entity_test');
Dries Buytaert
committed
$entity->field_test_taxonomy->target_id = $this->term->id();
Alex Pott
committed
$entity->name->value = $this->randomMachineName();
Angie Byron
committed
$entity->save();
$entity = entity_load('entity_test', $entity->id());
$this->assertTrue($entity->field_test_taxonomy instanceof FieldItemListInterface, 'Field implements interface.');
Angie Byron
committed
$this->assertTrue($entity->field_test_taxonomy[0] instanceof FieldItemInterface, 'Field item implements interface.');
$this->assertEqual($entity->field_test_taxonomy->target_id, $this->term->id(), 'Field item contains the expected TID.');
Alex Pott
committed
$this->assertEqual($entity->field_test_taxonomy->entity->getName(), $this->term->getName(), 'Field item entity contains the expected name.');
$this->assertEqual($entity->field_test_taxonomy->entity->id(), $tid, 'Field item entity contains the expected ID.');
$this->assertEqual($entity->field_test_taxonomy->entity->uuid(), $this->term->uuid(), 'Field item entity contains the expected UUID.');
Angie Byron
committed
// Change the name of the term via the reference.
Alex Pott
committed
$new_name = $this->randomMachineName();
Alex Pott
committed
$entity->field_test_taxonomy->entity->setName($new_name);
Angie Byron
committed
$entity->field_test_taxonomy->entity->save();
// Verify it is the correct name.
$term = Term::load($tid);
Alex Pott
committed
$this->assertEqual($term->getName(), $new_name, 'The name of the term was changed.');
Angie Byron
committed
// Make sure the computed term reflects updates to the term id.
$term2 = entity_create('taxonomy_term', array(
Alex Pott
committed
'name' => $this->randomMachineName(),
Alex Pott
committed
'vid' => $this->term->getVocabularyId(),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
Angie Byron
committed
));
$term2->save();
Dries Buytaert
committed
$entity->field_test_taxonomy->target_id = $term2->id();
$this->assertEqual($entity->field_test_taxonomy->entity->id(), $term2->id(), 'Field item entity contains the new TID.');
Alex Pott
committed
$this->assertEqual($entity->field_test_taxonomy->entity->getName(), $term2->getName(), 'Field item entity contains the new name.');
// Test sample item generation.
$entity = entity_create('entity_test');
$entity->field_test_taxonomy->generateSampleItems();
$this->entityValidateAndSave($entity);