Browse Source

Fix tests

3.1.x
Chi 3 years ago
parent
commit
6b9c4f3463
  1. 45
      tests/src/Kernel/AbstractTestCase.php
  2. 5
      tests/src/Kernel/CacheMetadataExtractorTest.php
  3. 11
      tests/src/Kernel/EntityFormViewBuilderTest.php
  4. 11
      tests/src/Kernel/EntityViewBuilderTest.php
  5. 9
      tests/src/Kernel/FieldViewBuilderTest.php
  6. 13
      tests/src/Kernel/ImageViewBuilderTest.php
  7. 9
      tests/src/Kernel/RegionViewBuilderTest.php

45
tests/src/Kernel/AbstractTestCase.php

@ -0,0 +1,45 @@
<?php
namespace Drupal\Tests\twig_tweak\Kernel;
use Drupal\KernelTests\KernelTestBase;
use PHPUnit\Framework\Assert as PHPUnitAssert;
/**
* A base class for Twig Tweak kernel tests.
*/
abstract class AbstractTestCase extends KernelTestBase {
/**
* Asserts cache metadata.
*/
protected static function assertCache(array $expected_cache, array $actual_cache): void {
self::sortCache($expected_cache);
self::sortCache($actual_cache);
PHPUnitAssert::assertSame($expected_cache, $actual_cache);
}
/**
* Asserts render array.
*/
protected static function assertRenderArray(array $expected_build, array $actual_build): void {
self::sortCache($expected_build['#cache']);
self::sortCache($actual_build['#cache']);
PHPUnitAssert::assertSame($expected_build, $actual_build);
}
/**
* Sort cache metadata.
*
* @see https://www.drupal.org/node/3230171
*/
private static function sortCache(array &$cache): void {
if (\array_key_exists('tags', $cache)) {
sort($cache['tags']);
}
if (\array_key_exists('contexts', $cache)) {
sort($cache['contexts']);
}
}
}

5
tests/src/Kernel/CacheMetadataExtractorTest.php

@ -3,14 +3,13 @@
namespace Drupal\Tests\twig_tweak\Kernel;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\KernelTests\KernelTestBase;
/**
* A test for Cache Metadata Extractor service.
*
* @group twig_tweak
*/
final class CacheMetadataExtractorTest extends KernelTestBase {
final class CacheMetadataExtractorTest extends AbstractTestCase {
/**
* {@inheritdoc}
@ -88,7 +87,7 @@ final class CacheMetadataExtractorTest extends KernelTestBase {
'max-age' => 10,
],
];
self::assertSame($expected_build, $build);
self::assertRenderArray($expected_build, $build);
// -- Wrong type.
self::expectErrorMessage('The input should be either instance of Drupal\Core\Cache\CacheableDependencyInterface or array. stdClass was given.');

11
tests/src/Kernel/EntityFormViewBuilderTest.php

@ -3,7 +3,6 @@
namespace Drupal\Tests\twig_tweak\Kernel;
use Drupal\Core\Cache\Cache;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\user\Traits\UserCreationTrait;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
@ -13,7 +12,7 @@ use Drupal\node\Entity\NodeType;
*
* @group twig_tweak
*/
final class EntityFormViewBuilderTest extends KernelTestBase {
final class EntityFormViewBuilderTest extends AbstractTestCase {
use UserCreationTrait;
@ -47,7 +46,7 @@ final class EntityFormViewBuilderTest extends KernelTestBase {
/**
* Test callback.
*/
public function testEntityViewBuilder(): void {
public function testEntityFormViewBuilder(): void {
$view_builder = $this->container->get('twig_tweak.entity_form_view_builder');
@ -82,7 +81,7 @@ final class EntityFormViewBuilderTest extends KernelTestBase {
],
'max-age' => 50,
];
self::assertSame($expected_cache, $build['#cache']);
self::assertCache($expected_cache, $build['#cache']);
self::assertStringContainsString('<form class="node-article-form node-form" ', $this->renderPlain($build));
// -- Private node with access check.
@ -100,7 +99,7 @@ final class EntityFormViewBuilderTest extends KernelTestBase {
],
'max-age' => 50,
];
self::assertSame($expected_cache, $build['#cache']);
self::assertCache($expected_cache, $build['#cache']);
self::assertSame('', $this->renderPlain($build));
// -- Private node without access check.
@ -117,7 +116,7 @@ final class EntityFormViewBuilderTest extends KernelTestBase {
],
'max-age' => Cache::PERMANENT,
];
self::assertSame($expected_cache, $build['#cache']);
self::assertCache($expected_cache, $build['#cache']);
self::assertStringContainsString('<form class="node-article-form node-form" ', $this->renderPlain($build));
}

11
tests/src/Kernel/EntityViewBuilderTest.php

@ -3,7 +3,6 @@
namespace Drupal\Tests\twig_tweak\Kernel;
use Drupal\Core\Cache\Cache;
use Drupal\KernelTests\KernelTestBase;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\user\Traits\UserCreationTrait;
@ -13,7 +12,7 @@ use Drupal\Tests\user\Traits\UserCreationTrait;
*
* @group twig_tweak
*/
final class EntityViewBuilderTest extends KernelTestBase {
final class EntityViewBuilderTest extends AbstractTestCase {
use UserCreationTrait;
@ -84,7 +83,7 @@ final class EntityViewBuilderTest extends KernelTestBase {
],
'bin' => 'render',
];
self::assertSame($expected_cache, $build['#cache']);
self::assertCache($expected_cache, $build['#cache']);
$expected_html = <<< 'HTML'
<article role="article">
@ -117,7 +116,7 @@ final class EntityViewBuilderTest extends KernelTestBase {
],
'bin' => 'render',
];
self::assertSame($expected_cache, $build['#cache']);
self::assertCache($expected_cache, $build['#cache']);
$expected_html = <<< 'HTML'
<article role="article">
@ -150,7 +149,7 @@ final class EntityViewBuilderTest extends KernelTestBase {
],
'max-age' => 50,
];
self::assertSame($expected_cache, $build['#cache']);
self::assertCache($expected_cache, $build['#cache']);
self::assertSame('', $this->renderPlain($build));
@ -172,7 +171,7 @@ final class EntityViewBuilderTest extends KernelTestBase {
],
'bin' => 'render',
];
self::assertSame($expected_cache, $build['#cache']);
self::assertCache($expected_cache, $build['#cache']);
$expected_html = <<< 'HTML'
<article role="article">

9
tests/src/Kernel/FieldViewBuilderTest.php

@ -3,7 +3,6 @@
namespace Drupal\Tests\twig_tweak\Kernel;
use Drupal\Core\Cache\Cache;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\user\Traits\UserCreationTrait;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
@ -13,7 +12,7 @@ use Drupal\node\Entity\NodeType;
*
* @group twig_tweak
*/
final class FieldViewBuilderTest extends KernelTestBase {
final class FieldViewBuilderTest extends AbstractTestCase {
use UserCreationTrait;
@ -74,7 +73,7 @@ final class FieldViewBuilderTest extends KernelTestBase {
],
'max-age' => 50,
];
self::assertSame($expected_cache, $build['#cache']);
self::assertCache($expected_cache, $build['#cache']);
self::assertSame('<span>Public node</span>', $this->renderPlain($build));
@ -93,7 +92,7 @@ final class FieldViewBuilderTest extends KernelTestBase {
],
'max-age' => 50,
];
self::assertSame($expected_cache, $build['#cache']);
self::assertCache($expected_cache, $build['#cache']);
$expected_html = '<span><a href="/node/1" hreflang="en">Public node</a></span>';
self::assertSame($expected_html, $this->renderPlain($build));
@ -112,7 +111,7 @@ final class FieldViewBuilderTest extends KernelTestBase {
],
'max-age' => 50,
];
self::assertSame($expected_cache, $build['#cache']);
self::assertCache($expected_cache, $build['#cache']);
self::assertSame('', $this->renderPlain($build));
// -- Private node without access check.

13
tests/src/Kernel/ImageViewBuilderTest.php

@ -6,7 +6,6 @@ use Drupal\Core\Cache\Cache;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\file\Entity\File;
use Drupal\image\Entity\ImageStyle;
use Drupal\KernelTests\KernelTestBase;
use Drupal\responsive_image\Entity\ResponsiveImageStyle;
/**
@ -14,7 +13,7 @@ use Drupal\responsive_image\Entity\ResponsiveImageStyle;
*
* @group twig_tweak
*/
final class ImageViewBuilderTest extends KernelTestBase {
final class ImageViewBuilderTest extends AbstractTestCase {
/**
* {@inheritdoc}
@ -80,7 +79,7 @@ final class ImageViewBuilderTest extends KernelTestBase {
'max-age' => 70,
],
];
self::assertSame($expected_build, $build);
self::assertRenderArray($expected_build, $build);
self::assertSame('<img src="/files/ocean.jpg" alt="" />', $this->renderPlain($build));
// -- With style.
@ -99,7 +98,7 @@ final class ImageViewBuilderTest extends KernelTestBase {
'max-age' => 70,
],
];
self::assertSame($expected_build, $build);
self::assertRenderArray($expected_build, $build);
self::assertSame('<img alt="Ocean" src="/files/styles/large/public/ocean.jpg?itok=abc" />', $this->renderPlain($build));
// -- With responsive style.
@ -118,7 +117,7 @@ final class ImageViewBuilderTest extends KernelTestBase {
'max-age' => 70,
],
];
self::assertSame($expected_build, $build);
self::assertRenderArray($expected_build, $build);
self::assertSame('<picture><img src="/files/ocean.jpg" alt="Ocean" /></picture>', $this->renderPlain($build));
// -- Private image with access check.
@ -130,7 +129,7 @@ final class ImageViewBuilderTest extends KernelTestBase {
'max-age' => 70,
],
];
self::assertSame($expected_build, $build);
self::assertRenderArray($expected_build, $build);
self::assertSame('', $this->renderPlain($build));
// -- Private image without access check.
@ -145,7 +144,7 @@ final class ImageViewBuilderTest extends KernelTestBase {
'max-age' => Cache::PERMANENT,
],
];
self::assertSame($expected_build, $build);
self::assertRenderArray($expected_build, $build);
self::assertSame('<img src="/files/sea.jpg" alt="" />', $this->renderPlain($build));
}

9
tests/src/Kernel/RegionViewBuilderTest.php

@ -5,7 +5,6 @@ namespace Drupal\Tests\twig_tweak\Kernel;
use Drupal\block\Entity\Block;
use Drupal\Component\Utility\Html;
use Drupal\Core\Cache\Cache;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\user\Traits\UserCreationTrait;
/**
@ -13,7 +12,7 @@ use Drupal\Tests\user\Traits\UserCreationTrait;
*
* @group twig_tweak
*/
final class RegionViewBuilderTest extends KernelTestBase {
final class RegionViewBuilderTest extends AbstractTestCase {
use UserCreationTrait;
@ -117,7 +116,8 @@ final class RegionViewBuilderTest extends KernelTestBase {
'max-age' => 123,
],
];
self::assertSame($expected_build, $build);
self::assertRenderArray($expected_build, $build);
$expected_html = <<< 'HTML'
<div>
@ -137,7 +137,8 @@ final class RegionViewBuilderTest extends KernelTestBase {
->save();
$build = $view_builder->build('sidebar_first');
self::assertSame($expected_build, $build);
self::assertRenderArray($expected_build, $build);
Html::resetSeenIds();
$actual_html = $renderer->renderPlain($expected_build);
self::assertSame(self::normalizeHtml($expected_html), self::normalizeHtml($actual_html));

Loading…
Cancel
Save