You are here

private function ContextBlockTest::doEntityTest in Core Context 8

Tests viewing context values attached to a fieldable entity.

Parameters

\Drupal\Core\Entity\FieldableEntityInterface $entity: The entity to view.

4 calls to ContextBlockTest::doEntityTest()
ContextBlockTest::testMedia in tests/src/Functional/ContextBlockTest.php
Tests viewing context values stored on a media item.
ContextBlockTest::testNode in tests/src/Functional/ContextBlockTest.php
Tests viewing context values stored on a node.
ContextBlockTest::testTaxonomyTerm in tests/src/Functional/ContextBlockTest.php
Tests viewing context values stored on a taxonomy term.
ContextBlockTest::testUserAccount in tests/src/Functional/ContextBlockTest.php
Tests viewing context values stored on a user account.

File

tests/src/Functional/ContextBlockTest.php, line 80

Class

ContextBlockTest
@group core_context

Namespace

Drupal\Tests\core_context\Functional

Code

private function doEntityTest(FieldableEntityInterface $entity) {
  $entity_type_id = $entity
    ->getEntityTypeId();
  $bundle = $entity
    ->bundle();
  $storage = FieldStorageConfig::create([
    'entity_type' => $entity_type_id,
    'field_name' => 'context',
    'type' => 'context',
    'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
  ]);
  $storage
    ->save();
  FieldConfig::create([
    'field_storage' => $storage,
    'bundle' => $bundle,
  ])
    ->save();
  $this->container
    ->get('entity_display.repository')
    ->getViewDisplay($entity_type_id, $bundle, 'full')
    ->setThirdPartySetting('core_context', 'contexts', [
    'letter' => [
      'type' => 'string',
      'label' => 'NATO phonetic letter of the day',
      'description' => 'One of the words in the NATO phonetic alphabet',
      'value' => 'Romeo',
    ],
  ])
    ->save();
  $entity = $this
    ->reload($entity);
  $entity
    ->get('context')
    ->appendItem([
    'id' => 'value',
    'type' => 'integer',
    'label' => 'Age of Methuselah',
    'description' => 'This is how old Methuselah was when he died.',
    'value' => 969,
  ]);
  $this
    ->assertCount(0, $entity
    ->validate());
  $this
    ->assertSame(SAVED_UPDATED, $entity
    ->save());

  // Reload the entity to be certain that the context field actually has
  // data in it.
  $entity = $this
    ->reload($entity);

  /** @var \Drupal\core_context\Plugin\Field\FieldType\ContextItem $item */
  $item = $entity
    ->get('context');
  $this
    ->assertFalse($item
    ->isEmpty(), 'Context field is empty.');
  $this
    ->assertSame('value', $item->id);
  $this
    ->assertSame('integer', $item->type);
  $this
    ->assertSame('Age of Methuselah', $item->label);
  $this
    ->assertSame('This is how old Methuselah was when he died.', $item->description);
  $this
    ->assertSame(969, $item->value);
  $this
    ->drupalLogin($this->rootUser);
  $this
    ->drupalGet($entity
    ->toUrl());
  $assert_session = $this
    ->assertSession();
  $assert_session
    ->statusCodeEquals(200);
  $assert_session
    ->pageTextContains($entity
    ->label());
  $assert_session
    ->pageTextContains('The context value is 969, brought to you by the letter Romeo.');
  $this
    ->assertCacheContext('route');
}