LinkItemTest.php 3 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Contains \Drupal\link\Tests\LinkItemTest.
 */

namespace Drupal\link\Tests;

10 11
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FieldItemInterface;
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
use Drupal\field\Tests\FieldUnitTestBase;

/**
 * Tests the new entity API for the link field type.
 */
class LinkItemTest extends FieldUnitTestBase {

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

  public static function getInfo() {
    return array(
      'name' => 'Link field item',
      'description' => 'Tests the new entity API for the link field type.',
      'group' => 'Field types',
    );
  }

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

    // Create an link field and instance for validation.
38
    entity_create('field_entity', array(
39 40
      'name' => 'field_test',
      'entity_type' => 'entity_test',
41
      'type' => 'link',
42 43
    ))->save();
    entity_create('field_instance', array(
44 45 46
      'entity_type' => 'entity_test',
      'field_name' => 'field_test',
      'bundle' => 'entity_test',
47
    ))->save();
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
  }

  /**
   * Tests using entity fields of the link field type.
   */
  public function testLinkItem() {
    // Create entity.
    $entity = entity_create('entity_test', array());
    $url = 'http://www.drupal.org';
    $title = $this->randomName();
    $class = $this->randomName();
    $entity->field_test->url = $url;
    $entity->field_test->title = $title;
    $entity->field_test->get('attributes')->set('class', $class);
    $entity->name->value = $this->randomName();
    $entity->save();

    // Verify that the field value is changed.
    $id = $entity->id();
    $entity = entity_load('entity_test', $id);
68
    $this->assertTrue($entity->field_test instanceof FieldItemListInterface, 'Field implements interface.');
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
    $this->assertTrue($entity->field_test[0] instanceof FieldItemInterface, 'Field item implements interface.');
    $this->assertEqual($entity->field_test->url, $url);
    $this->assertEqual($entity->field_test[0]->url, $url);
    $this->assertEqual($entity->field_test->title, $title);
    $this->assertEqual($entity->field_test[0]->title, $title);
    $this->assertEqual($entity->field_test->attributes['class'], $class);

    // Verify changing the field value.
    $new_url = 'http://drupal.org';
    $new_title = $this->randomName();
    $new_class = $this->randomName();
    $entity->field_test->url = $new_url;
    $entity->field_test->title = $new_title;
    $entity->field_test->get('attributes')->set('class', $new_class);
    $this->assertEqual($entity->field_test->url, $new_url);
    $this->assertEqual($entity->field_test->title, $new_title);
    $this->assertEqual($entity->field_test->attributes['class'], $new_class);

    // Read changed entity and assert changed values.
    $entity->save();
    $entity = entity_load('entity_test', $id);
    $this->assertEqual($entity->field_test->url, $new_url);
    $this->assertEqual($entity->field_test->title, $new_title);
    $this->assertEqual($entity->field_test->attributes['class'], $new_class);
  }

}