BooleanItemTest.php in Zircon Profile 8
File
core/modules/field/src/Tests/Boolean/BooleanItemTest.php
View source
<?php
namespace Drupal\field\Tests\Boolean;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\field\Tests\FieldUnitTestBase;
class BooleanItemTest extends FieldUnitTestBase {
protected function setUp() {
parent::setUp();
entity_create('field_storage_config', array(
'field_name' => 'field_boolean',
'entity_type' => 'entity_test',
'type' => 'boolean',
))
->save();
entity_create('field_config', array(
'entity_type' => 'entity_test',
'field_name' => 'field_boolean',
'bundle' => 'entity_test',
))
->save();
entity_get_form_display('entity_test', 'entity_test', 'default')
->setComponent('field_boolean', array(
'type' => 'boolean_checkbox',
))
->save();
}
public function testBooleanItem() {
$entity = entity_create('entity_test');
$value = '1';
$entity->field_boolean = $value;
$entity->name->value = $this
->randomMachineName();
$entity
->save();
$id = $entity
->id();
$entity = entity_load('entity_test', $id);
$this
->assertTrue($entity->field_boolean instanceof FieldItemListInterface, 'Field implements interface.');
$this
->assertTrue($entity->field_boolean[0] instanceof FieldItemInterface, 'Field item implements interface.');
$this
->assertEqual($entity->field_boolean->value, $value);
$this
->assertEqual($entity->field_boolean[0]->value, $value);
$new_value = 0;
$entity->field_boolean->value = $new_value;
$this
->assertEqual($entity->field_boolean->value, $new_value);
$entity
->save();
$entity = entity_load('entity_test', $id);
$this
->assertEqual($entity->field_boolean->value, $new_value);
$entity = entity_create('entity_test');
$entity->field_boolean
->generateSampleItems();
$this
->entityValidateAndSave($entity);
}
}