View source
<?php
namespace Drupal\Tests\core_context\Functional;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\media\Entity\Media;
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\media\Traits\MediaTypeCreationTrait;
use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait;
class ContextBlockTest extends BrowserTestBase {
use AssertPageCacheContextsAndTagsTrait;
use MediaTypeCreationTrait;
protected $defaultTheme = 'stark';
protected static $modules = [
'block',
'core_context',
'core_context_test',
'field',
'media',
'media_test_source',
'node',
'taxonomy',
'user',
];
protected function setUp() {
parent::setUp();
$this
->drupalPlaceBlock('context_block', [
'context_mapping' => [
'value' => '@core_context:value',
'letter' => '@core_context:letter',
],
]);
}
private function reload(EntityInterface $entity) {
return $this->container
->get('entity_type.manager')
->getStorage($entity
->getEntityTypeId())
->load($entity
->id());
}
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());
$entity = $this
->reload($entity);
$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');
}
public function testMedia() {
$this
->config('media.settings')
->set('standalone_url', TRUE)
->save();
$this->container
->get('router.builder')
->rebuild();
$media_type = $this
->createMediaType('test')
->id();
$media = Media::create([
'bundle' => $media_type,
'field_media_test' => $this
->randomString(),
]);
$media
->save();
$this
->doEntityTest($media);
}
public function testNode() {
$node_type = $this
->drupalCreateContentType()
->id();
$node = $this
->drupalCreateNode([
'type' => $node_type,
]);
$this
->doEntityTest($node);
}
public function testTaxonomyTerm() {
Vocabulary::create([
'vid' => 'tags',
'name' => 'Tags',
])
->save();
$term = Term::create([
'vid' => 'tags',
'name' => $this
->randomString(),
]);
$term
->save();
$this
->doEntityTest($term);
}
public function testUserAccount() {
$account = $this
->drupalCreateUser();
$this
->doEntityTest($account);
}
}