TermContextTest.php in Drupal 10
File
core/modules/taxonomy/tests/src/Kernel/ContextProvider/TermContextTest.php
View source
<?php
namespace Drupal\Tests\taxonomy\Kernel\ContextProvider;
use Drupal\Core\Routing\RouteMatch;
use Drupal\KernelTests\KernelTestBase;
use Drupal\taxonomy\ContextProvider\TermRouteContext;
use Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait;
class TermContextTest extends KernelTestBase {
use TaxonomyTestTrait;
protected static $modules = [
'filter',
'taxonomy',
'text',
'user',
];
protected function setUp() : void {
parent::setUp();
$this
->installConfig([
'filter',
]);
$this
->installEntitySchema('user');
$this
->installEntitySchema('taxonomy_term');
}
public function testGetAvailableContexts() {
$context_repository = $this->container
->get('context.repository');
$contexts = $context_repository
->getAvailableContexts();
$this
->assertArrayHasKey('@taxonomy_term.taxonomy_term_route_context:taxonomy_term', $contexts);
$this
->assertSame('entity:taxonomy_term', $contexts['@taxonomy_term.taxonomy_term_route_context:taxonomy_term']
->getContextDefinition()
->getDataType());
}
public function testGetRuntimeContexts() {
$vocabulary = $this
->createVocabulary();
$term = $this
->createTerm($vocabulary);
$url = $term
->toUrl();
$route_provider = \Drupal::service('router.route_provider');
$route = $route_provider
->getRouteByName($url
->getRouteName());
$route_match = new RouteMatch($url
->getRouteName(), $route, [
'taxonomy_term' => $term,
]);
$provider = new TermRouteContext($route_match);
$runtime_contexts = $provider
->getRuntimeContexts([]);
$this
->assertArrayHasKey('taxonomy_term', $runtime_contexts);
$this
->assertTrue($runtime_contexts['taxonomy_term']
->hasContextValue());
}
}