You are here

MachineNameItemTest.php in Machine name 8

File

tests/src/Kernel/MachineNameItemTest.php
View source
<?php

namespace Drupal\Tests\machine_name\Kernel;

use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\Tests\field\Kernel\FieldKernelTestBase;
use Drupal\field\Entity\FieldStorageConfig;

/**
 * Tests the new entity API for the machine_name field type.
 *
 * @group machine_name
 */
class MachineNameItemTest extends FieldKernelTestBase {

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = [
    'machine_name',
  ];

  /**
   * Sets the test up.
   */
  protected function setUp() {
    parent::setUp();

    // Create a machine_name field storage and field for validation.
    FieldStorageConfig::create(array(
      'field_name' => 'field_test',
      'entity_type' => 'entity_test',
      'type' => 'machine_name',
    ))
      ->save();
    FieldConfig::create([
      'entity_type' => 'entity_test',
      'field_name' => 'field_test',
      'bundle' => 'entity_test',
    ])
      ->save();
  }

  /**
   * Tests using entity fields of the machine_name field type.
   */
  public function testTestItem() {

    // Verify entity creation.
    $entity = EntityTest::create();
    $value = 'machine_name_test';
    $entity->field_test = $value;
    $entity->name->value = $this
      ->randomMachineName();
    $entity
      ->save();

    // Verify entity has been created properly.
    $id = $entity
      ->id();
    $entity = EntityTest::load($id);
    $this
      ->assertTrue($entity->field_test instanceof FieldItemListInterface, 'Field implements interface.');
    $this
      ->assertTrue($entity->field_test[0] instanceof FieldItemInterface, 'Field item implements interface.');
    $this
      ->assertEqual($entity->field_test->value, $value);
    $this
      ->assertEqual($entity->field_test[0]->value, $value);

    // Verify changing the field value.
    $new_value = 'changed_machine_name';
    $entity->field_test->value = $new_value;
    $this
      ->assertEqual($entity->field_test->value, $new_value);

    // Read changed entity and assert changed values.
    $entity
      ->save();
    $entity = EntityTest::load($id);
    $this
      ->assertEqual($entity->field_test->value, $new_value);

    // Test sample item generation.
    $entity = EntityTest::create();
    $entity->field_test
      ->generateSampleItems();
    $this
      ->entityValidateAndSave($entity);
  }

}

Classes

Namesort descending Description
MachineNameItemTest Tests the new entity API for the machine_name field type.