You are here

public function StateItemTest::testGenerateSampleValue in State Machine 8

@covers ::generateSampleValue

File

tests/src/Kernel/StateItemTest.php, line 151

Class

StateItemTest
@coversDefaultClass \Drupal\state_machine\Plugin\Field\FieldType\StateItem @group state_machine

Namespace

Drupal\Tests\state_machine\Kernel

Code

public function testGenerateSampleValue() {
  $entity = EntityTestWithBundle::create([
    'type' => 'first',
  ]);
  $entity->field_state
    ->generateSampleItems();

  /** @var \Drupal\state_machine\Plugin\Field\FieldType\StateItemInterface $state_item */
  $state_item = $entity
    ->get('field_state')
    ->first();
  $this
    ->assertEquals('default', $state_item
    ->getWorkflow()
    ->getId());
  $this
    ->assertNotEmpty($state_item
    ->getId());
  $this
    ->assertTrue(in_array($state_item
    ->getId(), array_keys($state_item
    ->getWorkflow()
    ->getStates())));
  $this
    ->entityValidateAndSave($entity);
  $entity = EntityTestWithBundle::create([
    'type' => 'second',
  ]);
  $entity->field_state
    ->generateSampleItems();

  /** @var \Drupal\state_machine\Plugin\Field\FieldType\StateItemInterface $state_item */
  $state_item = $entity
    ->get('field_state')
    ->first();
  $this
    ->assertNotEmpty($state_item
    ->getId());
  $this
    ->assertTrue(in_array($state_item
    ->getId(), array_keys($state_item
    ->getWorkflow()
    ->getStates())));
  $this
    ->entityValidateAndSave($entity);
  $entity = EntityTestWithBundle::create([
    'type' => 'third',
  ]);
  $entity->field_state
    ->generateSampleItems();

  /** @var \Drupal\state_machine\Plugin\Field\FieldType\StateItemInterface $state_item */
  $state_item = $entity
    ->get('field_state')
    ->first();
  $this
    ->assertEquals('two_transitions', $state_item
    ->getWorkflow()
    ->getId());
  $this
    ->assertNotEmpty($state_item
    ->getId());
  $this
    ->assertTrue(in_array($state_item
    ->getId(), array_keys($state_item
    ->getWorkflow()
    ->getStates())));
  $this
    ->entityValidateAndSave($entity);
}