<?php
namespace Drupal\link\Tests;
use Drupal\Core\Entity\Field\FieldInterface;
use Drupal\Core\Entity\Field\FieldItemInterface;
use Drupal\field\Tests\FieldUnitTestBase;
class LinkItemTest extends FieldUnitTestBase {
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();
$this->field = array(
'field_name' => 'field_test',
'type' => 'link',
);
field_create_field($this->field);
$this->instance = array(
'entity_type' => 'entity_test',
'field_name' => 'field_test',
'bundle' => 'entity_test',
);
field_create_instance($this->instance);
}
public function testLinkItem() {
$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();
$id = $entity
->id();
$entity = entity_load('entity_test', $id);
$this
->assertTrue($entity->field_test instanceof FieldInterface, 'Field implements interface.');
$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);
$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);
$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);
}
}