View source
<?php
namespace Drupal\Tests\preprocess_event_dispatcher\Unit;
use Drupal\block\BlockInterface;
use Drupal\block_content\BlockContentInterface;
use Drupal\preprocess_event_dispatcher\Event\AbstractPreprocessEvent;
use Drupal\preprocess_event_dispatcher\Event\BlockPreprocessEvent;
use Drupal\preprocess_event_dispatcher\Event\FieldPreprocessEvent;
use Drupal\preprocess_event_dispatcher\Event\FormPreprocessEvent;
use Drupal\preprocess_event_dispatcher\Event\HtmlPreprocessEvent;
use Drupal\preprocess_event_dispatcher\Event\ImagePreprocessEvent;
use Drupal\preprocess_event_dispatcher\Event\PagePreprocessEvent;
use Drupal\preprocess_event_dispatcher\Event\StatusMessagesPreprocessEvent;
use Drupal\preprocess_event_dispatcher\Event\UsernamePreprocessEvent;
use Drupal\preprocess_event_dispatcher\Event\ViewFieldPreprocessEvent;
use Drupal\preprocess_event_dispatcher\Event\ViewPreprocessEvent;
use Drupal\preprocess_event_dispatcher\Event\ViewTablePreprocessEvent;
use Drupal\preprocess_event_dispatcher\Variables\AbstractEventVariables;
use Drupal\preprocess_event_dispatcher\Variables\BlockEventVariables;
use Drupal\preprocess_event_dispatcher\Variables\FieldEventVariables;
use Drupal\preprocess_event_dispatcher\Variables\FormEventVariables;
use Drupal\preprocess_event_dispatcher\Variables\HtmlEventVariables;
use Drupal\preprocess_event_dispatcher\Variables\ImageEventVariables;
use Drupal\preprocess_event_dispatcher\Variables\PageEventVariables;
use Drupal\preprocess_event_dispatcher\Variables\StatusMessagesEventVariables;
use Drupal\preprocess_event_dispatcher\Variables\UsernameEventVariables;
use Drupal\preprocess_event_dispatcher\Variables\ViewEventVariables;
use Drupal\preprocess_event_dispatcher\Variables\ViewFieldEventVariables;
use Drupal\preprocess_event_dispatcher\Variables\ViewTableEventVariables;
use Drupal\Tests\preprocess_event_dispatcher\Unit\Helpers\YamlDefinitionsLoader;
use PHPUnit\Framework\TestCase;
use Drupal\user\UserInterface;
use Drupal\views\Plugin\views\field\FieldHandlerInterface;
use Drupal\views\Plugin\views\field\Markup;
use Drupal\views\ResultRow;
use Drupal\views\ViewExecutable;
use Mockery;
final class OtherEventVariablesTest extends TestCase {
private $mapper;
public function setUp() : void {
$this->mapper = YamlDefinitionsLoader::getInstance()
->getMapper();
}
public function testBlockEvent() : void {
$variablesArray = $this
->createVariablesArray();
$block = Mockery::mock(BlockInterface::class);
$variablesArray['block'] = $block;
$variablesArray['elements']['#id'] = '22';
$variablesArray['content']['test'] = [
'success2',
];
$variables = $this
->getVariablesFromCreatedEvent(BlockPreprocessEvent::class, $variablesArray);
self::assertInstanceOf(BlockEventVariables::class, $variables);
$this
->assertAbstractEventVariables($variables);
self::assertSame($block, $variables
->getBlock());
self::assertSame('22', $variables
->getId());
self::assertSame([
'success2',
], $variables
->getContentChild('test'));
self::assertSame([], $variables
->getContentChild('none-existing'));
self::assertNull($variables
->getBlockContent());
}
public function testBlockEventWithBlockContent() : void {
$variablesArray = $this
->createVariablesArray();
$blockContent = Mockery::mock(BlockContentInterface::class);
$variablesArray['content']['#block_content'] = $blockContent;
$variables = $this
->getVariablesFromCreatedEvent(BlockPreprocessEvent::class, $variablesArray);
self::assertInstanceOf(BlockEventVariables::class, $variables);
$this
->assertAbstractEventVariables($variables);
self::assertSame($blockContent, $variables
->getBlockContent());
}
public function testFieldEvent() : void {
$variablesArray = $this
->createVariablesArray();
$variablesArray['element'] = [
'element array',
];
$variablesArray['items'] = [
'items array',
];
$variables = $this
->getVariablesFromCreatedEvent(FieldPreprocessEvent::class, $variablesArray);
self::assertInstanceOf(FieldEventVariables::class, $variables);
$this
->assertAbstractEventVariables($variables);
self::assertSame([
'element array',
], $variables
->getElement());
self::assertSame([
'items array',
], $variables
->getItems());
}
public function testFormEvent() : void {
$variablesArray = $this
->createVariablesArray();
$variablesArray['element'] = [
'element array',
];
$variables = $this
->getVariablesFromCreatedEvent(FormPreprocessEvent::class, $variablesArray);
self::assertInstanceOf(FormEventVariables::class, $variables);
$this
->assertAbstractEventVariables($variables);
self::assertSame([
'element array',
], $variables
->getElement());
}
public function testHtmlEvent() : void {
$variablesArray = $this
->createVariablesArray();
$variables = $this
->getVariablesFromCreatedEvent(HtmlPreprocessEvent::class, $variablesArray);
self::assertInstanceOf(HtmlEventVariables::class, $variables);
$this
->assertAbstractEventVariables($variables);
}
public function testImageEvent() : void {
$variablesArray = $this
->createVariablesArray();
$variables = $this
->getVariablesFromCreatedEvent(ImagePreprocessEvent::class, $variablesArray);
self::assertInstanceOf(ImageEventVariables::class, $variables);
$this
->assertAbstractEventVariables($variables);
}
public function testPageEvent() : void {
$variablesArray = [
'page' => $this
->createVariablesArray(),
];
$variables = $this
->getVariablesFromCreatedEvent(PagePreprocessEvent::class, $variablesArray);
self::assertInstanceOf(PageEventVariables::class, $variables);
$this
->assertAbstractEventVariables($variables);
}
public function testUsernameEvent() : void {
$variablesArray = $this
->createVariablesArray();
$accountMock = Mockery::mock(UserInterface::class);
$accountMock
->expects('isAnonymous')
->with()
->once()
->andReturnTrue();
$variablesArray['account'] = $accountMock;
$variables = $this
->getVariablesFromCreatedEvent(UsernamePreprocessEvent::class, $variablesArray);
self::assertInstanceOf(UsernameEventVariables::class, $variables);
$this
->assertAbstractEventVariables($variables);
self::assertSame($accountMock, $variables
->getAccount());
self::assertTrue($variables
->userIsAnonymous());
}
public function testViewFieldEvent() : void {
$variablesArray = $this
->createVariablesArray();
$field = Mockery::mock(FieldHandlerInterface::class);
$variablesArray['field'] = $field;
$output = Mockery::mock(Markup::class);
$variablesArray['output'] = $output;
$row = Mockery::mock(ResultRow::class);
$variablesArray['row'] = $row;
$view = Mockery::mock(ViewExecutable::class);
$variablesArray['view'] = $view;
$variables = $this
->getVariablesFromCreatedEvent(ViewFieldPreprocessEvent::class, $variablesArray);
self::assertInstanceOf(ViewFieldEventVariables::class, $variables);
$this
->assertAbstractEventVariables($variables);
self::assertSame($field, $variables
->getField());
self::assertSame($output, $variables
->getOutput());
self::assertSame($row, $variables
->getRow());
self::assertSame($view, $variables
->getView());
}
public function testViewTableEvent() : void {
$variablesArray = $this
->createVariablesArray();
$rows = Mockery::mock(ResultRow::class);
$variablesArray['rows'] = $rows;
$view = Mockery::mock(ViewExecutable::class);
$variablesArray['view'] = $view;
$variables = $this
->getVariablesFromCreatedEvent(ViewTablePreprocessEvent::class, $variablesArray);
self::assertInstanceOf(ViewTableEventVariables::class, $variables);
$this
->assertAbstractEventVariables($variables);
self::assertSame($rows, $variables
->getRows());
self::assertSame($view, $variables
->getView());
}
public function testViewEvent() : void {
$variablesArray = $this
->createVariablesArray();
$variablesArray['rows'] = [
[
'#rows' => [
'rows',
],
],
];
$view = Mockery::mock(ViewExecutable::class);
$variablesArray['view'] = $view;
$variables = $this
->getVariablesFromCreatedEvent(ViewPreprocessEvent::class, $variablesArray);
self::assertInstanceOf(ViewEventVariables::class, $variables);
$this
->assertAbstractEventVariables($variables);
self::assertSame([
'rows',
], $variables
->getRows());
self::assertSame($view, $variables
->getView());
}
public function testStatusMessagesEvent() : void {
$variablesArray = $this
->createVariablesArray();
$variables = $this
->getVariablesFromCreatedEvent(StatusMessagesPreprocessEvent::class, $variablesArray);
self::assertInstanceOf(StatusMessagesEventVariables::class, $variables);
$this
->assertAbstractEventVariables($variables);
}
private function assertAbstractEventVariables(AbstractEventVariables $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) : AbstractEventVariables {
$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($class, $event);
return $event
->getVariables();
}
private function createVariablesArray() : array {
return [
'test' => 'success',
'reference' => 'first',
];
}
}