View source
<?php
namespace Drupal\Tests\preprocess_event_dispatcher\Unit;
use Drupal\comment\CommentInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\eck\EckEntityInterface;
use Drupal\node\NodeInterface;
use Drupal\paragraphs\ParagraphInterface;
use Drupal\preprocess_event_dispatcher\Event\AbstractPreprocessEvent;
use Drupal\preprocess_event_dispatcher\Event\CommentPreprocessEvent;
use Drupal\preprocess_event_dispatcher\Event\EckEntityPreprocessEvent;
use Drupal\preprocess_event_dispatcher\Event\NodePreprocessEvent;
use Drupal\preprocess_event_dispatcher\Event\ParagraphPreprocessEvent;
use Drupal\preprocess_event_dispatcher\Event\PreprocessEntityEventInterface;
use Drupal\preprocess_event_dispatcher\Event\TaxonomyTermPreprocessEvent;
use Drupal\preprocess_event_dispatcher\Variables\AbstractEntityEventVariables;
use Drupal\preprocess_event_dispatcher\Variables\CommentEventVariables;
use Drupal\preprocess_event_dispatcher\Variables\EckEntityEventVariables;
use Drupal\preprocess_event_dispatcher\Variables\NodeEventVariables;
use Drupal\preprocess_event_dispatcher\Variables\ParagraphEventVariables;
use Drupal\preprocess_event_dispatcher\Variables\TaxonomyTermEventVariables;
use Drupal\taxonomy\TermInterface;
use Drupal\Tests\preprocess_event_dispatcher\Unit\Helpers\EntityMockFactory;
use Drupal\Tests\preprocess_event_dispatcher\Unit\Helpers\YamlDefinitionsLoader;
use PHPUnit\Framework\TestCase;
use Mockery;
final class EntityEventVariablesTest extends TestCase {
private $mapper;
public function setUp() : void {
$this->mapper = YamlDefinitionsLoader::getInstance()
->getMapper();
}
public function testCommentEvent() : void {
$comment = EntityMockFactory::getMock(CommentInterface::class, 'comment', 'comment_bundle', 'comment_view_mode');
$commentedEntity = Mockery::mock(ContentEntityInterface::class);
$variablesArray = $this
->createVariablesArray();
$variablesArray['comment'] = $comment;
$variablesArray['commented_entity'] = $commentedEntity;
$variablesArray['view_mode'] = $comment
->getViewMode();
$variables = $this
->getVariablesFromCreatedEvent(CommentPreprocessEvent::class, $variablesArray);
self::assertInstanceOf(CommentEventVariables::class, $variables);
$this
->assertAbstractEntityEventVariables($variables, $comment);
self::assertSame($comment, $variables
->getComment());
self::assertSame($commentedEntity, $variables
->getCommentedEntity());
}
public function testEckEntityEvent() : void {
$eckEntity = EntityMockFactory::getMock(EckEntityInterface::class, '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);
self::assertInstanceOf(EckEntityEventVariables::class, $variables);
$this
->assertAbstractEntityEventVariables($variables, $eckEntity);
self::assertSame($eckEntity, $variables
->getEckEntity());
}
public function testNodeEvent() : void {
$node = EntityMockFactory::getMock(NodeInterface::class, '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);
self::assertInstanceOf(NodeEventVariables::class, $variables);
$this
->assertAbstractEntityEventVariables($variables, $node);
self::assertSame($node, $variables
->getNode());
}
public function testParagraphEvent() : void {
$paragraph = EntityMockFactory::getMock(ParagraphInterface::class, '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);
self::assertInstanceOf(ParagraphEventVariables::class, $variables);
$this
->assertAbstractEntityEventVariables($variables, $paragraph);
self::assertSame($paragraph, $variables
->getParagraph());
}
public function testTaxonomyTermEvent() : void {
$term = EntityMockFactory::getMock(TermInterface::class, '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);
self::assertInstanceOf(TaxonomyTermEventVariables::class, $variables);
$this
->assertAbstractEntityEventVariables($variables, $term);
self::assertSame($term, $variables
->getTerm());
}
private function assertAbstractEntityEventVariables(AbstractEntityEventVariables $variables, EntityInterface $entity) : void {
$this
->assertAbstractEventVariables($variables);
self::assertSame($entity, $variables
->getEntity());
self::assertSame($entity
->getEntityType(), $variables
->getEntityType());
self::assertSame($entity
->bundle(), $variables
->getEntityBundle());
self::assertSame($entity
->getViewMode(), $variables
->getViewMode());
}
private function assertAbstractEventVariables(AbstractEntityEventVariables $variables) : void {
self::assertSame('success', $variables
->get('test'));
self::assertSame('default', $variables
->get('test2', 'default'));
$reference =& $variables
->getByReference('reference');
self::assertSame('first', $reference);
$reference = 'second';
self::assertSame('second', $variables
->get('reference'));
$variables
->set('test3', 'new set');
self::assertSame('new set', $variables
->get('test3'));
$variables
->remove('test');
self::assertNull($variables
->get('test'));
}
private function getVariablesFromCreatedEvent(string $class, array $variablesArray) : AbstractEntityEventVariables {
$hook = $class::getHook();
self::assertSame(AbstractPreprocessEvent::DISPATCH_NAME_PREFIX . $hook, $class::name());
$factory = $this->mapper
->getFactory($hook);
self::assertSame($hook, $factory
->getEventHook());
$event = $factory
->createEvent($variablesArray);
self::assertInstanceOf(PreprocessEntityEventInterface::class, $event);
self::assertInstanceOf($class, $event);
$variables = $event
->getVariables();
self::assertInstanceOf(AbstractEntityEventVariables::class, $variables);
return $variables;
}
private function createVariablesArray() : array {
return [
'test' => 'success',
'reference' => 'first',
];
}
}