View source
<?php
namespace Drupal\Tests\hook_event_dispatcher\Unit\Preprocess;
use Drupal\hook_event_dispatcher\Event\Preprocess\AbstractPreprocessEvent;
use Drupal\hook_event_dispatcher\Event\Preprocess\CommentPreprocessEvent;
use Drupal\hook_event_dispatcher\Event\Preprocess\EckEntityPreprocessEvent;
use Drupal\hook_event_dispatcher\Event\Preprocess\NodePreprocessEvent;
use Drupal\hook_event_dispatcher\Event\Preprocess\ParagraphPreprocessEvent;
use Drupal\hook_event_dispatcher\Event\Preprocess\PreprocessEntityEventInterface;
use Drupal\hook_event_dispatcher\Event\Preprocess\TaxonomyTermPreprocessEvent;
use Drupal\hook_event_dispatcher\Event\Preprocess\Variables\AbstractEntityEventVariables;
use Drupal\hook_event_dispatcher\Event\Preprocess\Variables\CommentEventVariables;
use Drupal\hook_event_dispatcher\Event\Preprocess\Variables\EckEntityEventVariables;
use Drupal\hook_event_dispatcher\Event\Preprocess\Variables\NodeEventVariables;
use Drupal\hook_event_dispatcher\Event\Preprocess\Variables\ParagraphEventVariables;
use Drupal\hook_event_dispatcher\Event\Preprocess\Variables\TaxonomyTermEventVariables;
use Drupal\Tests\hook_event_dispatcher\Unit\Preprocess\Helpers\EntityMock;
use Drupal\Tests\hook_event_dispatcher\Unit\Preprocess\Helpers\YamlDefinitionsLoader;
use Drupal\Tests\UnitTestCase;
final class EntityEventVariablesTest extends UnitTestCase {
private $mapper;
public function setUp() {
$this->mapper = YamlDefinitionsLoader::getInstance()
->getMapper();
}
public function testCommentEvent() {
$comment = new EntityMock('comment', 'comment_bundle', 'comment_view_mode');
$variablesArray = $this
->createVariablesArray();
$variablesArray['comment'] = $comment;
$variablesArray['commented_entity'] = 'node';
$variablesArray['view_mode'] = $comment
->getViewMode();
$variables = $this
->getVariablesFromCreatedEvent(CommentPreprocessEvent::class, $variablesArray);
self::assertInstanceOf(CommentEventVariables::class, $variables);
$this
->assertAbstractEntityEventVariables($variables, $comment);
$this
->assertSame($comment, $variables
->getComment());
$this
->assertSame('node', $variables
->getCommentedEntity());
}
public function testEckEntityEvent() {
$eckEntity = new EntityMock('eck_entity', 'eck_entity_bundle', 'eck_entity_view_mode');
$variablesArray = $this
->createVariablesArray();
$variablesArray['eck_entity'] = $eckEntity;
$variablesArray['elements'] = [
'#view_mode' => $eckEntity
->getViewMode(),
];
$variablesArray['theme_hook_original'] = $eckEntity
->getEntityType();
$variablesArray['bundle'] = $eckEntity
->bundle();
$variables = $this
->getVariablesFromCreatedEvent(EckEntityPreprocessEvent::class, $variablesArray);
$this
->assertInstanceOf(EckEntityEventVariables::class, $variables);
$this
->assertAbstractEntityEventVariables($variables, $eckEntity);
$this
->assertSame($eckEntity, $variables
->getEckEntity());
}
public function testNodeEvent() {
$node = new EntityMock('node', 'node_bundle', 'node_view_mode');
$variablesArray = $this
->createVariablesArray();
$variablesArray['node'] = $node;
$variablesArray['theme_hook_original'] = $node
->getEntityType();
$variablesArray['view_mode'] = $node
->getViewMode();
$variables = $this
->getVariablesFromCreatedEvent(NodePreprocessEvent::class, $variablesArray);
$this
->assertInstanceOf(NodeEventVariables::class, $variables);
$this
->assertAbstractEntityEventVariables($variables, $node);
$this
->assertSame($node, $variables
->getNode());
}
public function testParagraphEvent() {
$paragraph = new EntityMock('paragraph', 'paragraph_bundle', 'paragraph_view_mode');
$variablesArray = $this
->createVariablesArray();
$variablesArray['paragraph'] = $paragraph;
$variablesArray['theme_hook_original'] = $paragraph
->getEntityType();
$variablesArray['view_mode'] = $paragraph
->getViewMode();
$variables = $this
->getVariablesFromCreatedEvent(ParagraphPreprocessEvent::class, $variablesArray);
$this
->assertInstanceOf(ParagraphEventVariables::class, $variables);
$this
->assertAbstractEntityEventVariables($variables, $paragraph);
$this
->assertSame($paragraph, $variables
->getParagraph());
}
public function testTaxonomyTermEvent() {
$term = new EntityMock('taxonomy_term', 'term_bundle', 'term_view_mode');
$variablesArray = $this
->createVariablesArray();
$variablesArray['term'] = $term;
$variablesArray['theme_hook_original'] = $term
->getEntityType();
$variablesArray['view_mode'] = $term
->getViewMode();
$variables = $this
->getVariablesFromCreatedEvent(TaxonomyTermPreprocessEvent::class, $variablesArray);
$this
->assertInstanceOf(TaxonomyTermEventVariables::class, $variables);
$this
->assertAbstractEntityEventVariables($variables, $term);
$this
->assertSame($term, $variables
->getTerm());
}
private function assertAbstractEntityEventVariables(AbstractEntityEventVariables $variables, EntityMock $entity) {
$this
->assertAbstractEventVariables($variables);
$this
->assertSame($entity, $variables
->getEntity());
$this
->assertSame($entity
->getEntityType(), $variables
->getEntityType());
$this
->assertSame($entity
->bundle(), $variables
->getEntityBundle());
$this
->assertSame($entity
->getViewMode(), $variables
->getViewMode());
}
private function assertAbstractEventVariables(AbstractEntityEventVariables $variables) {
$this
->assertEquals('success', $variables
->get('test'));
$this
->assertEquals('default', $variables
->get('test2', 'default'));
$reference =& $variables
->getByReference('reference');
$this
->assertEquals('first', $reference);
$reference = 'second';
$this
->assertEquals('second', $variables
->get('reference'));
$variables
->set('test3', 'new set');
$this
->assertEquals('new set', $variables
->get('test3'));
$variables
->remove('test');
$this
->assertNull($variables
->get('test'));
}
private function getVariablesFromCreatedEvent($class, array $variablesArray) {
$hook = $class::getHook();
$this
->assertEquals(AbstractPreprocessEvent::DISPATCH_NAME_PREFIX . $hook, $class::name());
$factory = $this->mapper
->getFactory($hook);
$this
->assertEquals($hook, $factory
->getEventHook());
$event = $factory
->createEvent($variablesArray);
$this
->assertInstanceOf(PreprocessEntityEventInterface::class, $event);
$this
->assertInstanceOf($class, $event);
$variables = $event
->getVariables();
$this
->assertInstanceOf(AbstractEntityEventVariables::class, $variables);
return $variables;
}
private function createVariablesArray() {
return [
'test' => 'success',
'reference' => 'first',
];
}
}