diff options
Diffstat (limited to 'core/tests')
9 files changed, 232 insertions, 12 deletions
diff --git a/core/tests/Drupal/KernelTests/Components/ComponentNegotiatorTest.php b/core/tests/Drupal/KernelTests/Components/ComponentNegotiatorTest.php index eb38e858f0d2..ffdf5e96bf4c 100644 --- a/core/tests/Drupal/KernelTests/Components/ComponentNegotiatorTest.php +++ b/core/tests/Drupal/KernelTests/Components/ComponentNegotiatorTest.php @@ -72,6 +72,7 @@ class ComponentNegotiatorTest extends ComponentKernelTestBase { '#type' => 'inline_template', '#template' => "{{ include('sdc_theme_test:my-card') }}", '#context' => ['header' => 'Foo bar'], + '#variant' => 'horizontal', ]; $crawler = $this->renderComponentRenderArray($build); $this->assertNotEmpty($crawler->filter('#sdc-wrapper .component--my-card--replaced__body')); diff --git a/core/tests/Drupal/KernelTests/Components/ComponentNodeVisitorTest.php b/core/tests/Drupal/KernelTests/Components/ComponentNodeVisitorTest.php index 6b72fa745ea5..d48694330727 100644 --- a/core/tests/Drupal/KernelTests/Components/ComponentNodeVisitorTest.php +++ b/core/tests/Drupal/KernelTests/Components/ComponentNodeVisitorTest.php @@ -22,6 +22,9 @@ class ComponentNodeVisitorTest extends ComponentKernelTestBase { */ protected static $themes = ['sdc_theme_test']; + const DEBUG_COMPONENT_ID_PATTERN = '/<!-- ([\n\s\S]*) Component start: ([\SA-Za-z+-:]+) -->/'; + const DEBUG_VARIANT_ID_PATTERN = '/<!-- [\n\s\S]* with variant: "([\SA-Za-z+-]+)" -->/'; + /** * Test that other visitors can modify Twig nodes. */ @@ -36,4 +39,37 @@ class ComponentNodeVisitorTest extends ComponentKernelTestBase { $this->assertTrue(TRUE); } + /** + * Test debug output for sdc components with component id and variant. + */ + public function testDebugRendersComponentStartWithVariant(): void { + // Enable twig theme debug to ensure that any + // changes to theme debugging format force checking + // that the auto paragraph filter continues to be applied + // correctly. + $twig = \Drupal::service('twig'); + $twig->enableDebug(); + + $build = [ + '#type' => 'component', + '#component' => 'sdc_theme_test:my-card', + '#variant' => 'vertical', + '#props' => [ + 'header' => 'My header', + ], + '#slots' => [ + 'card_body' => 'Foo bar', + ], + ]; + $crawler = $this->renderComponentRenderArray($build); + $content = $crawler->html(); + + $matches = []; + \preg_match_all(self::DEBUG_COMPONENT_ID_PATTERN, $content, $matches); + $this->assertSame($matches[2][0], 'sdc_theme_test:my-card'); + + \preg_match_all(self::DEBUG_VARIANT_ID_PATTERN, $content, $matches); + $this->assertSame($matches[1][0], 'vertical'); + } + } diff --git a/core/tests/Drupal/KernelTests/Components/ComponentRenderTest.php b/core/tests/Drupal/KernelTests/Components/ComponentRenderTest.php index 70a5e804af78..944d6cb145bc 100644 --- a/core/tests/Drupal/KernelTests/Components/ComponentRenderTest.php +++ b/core/tests/Drupal/KernelTests/Components/ComponentRenderTest.php @@ -96,7 +96,7 @@ class ComponentRenderTest extends ComponentKernelTestBase { $build = [ '#type' => 'inline_template', '#context' => ['content' => $content], - '#template' => "{% embed 'sdc_theme_test:my-card' with { header: 'Card header', content: content } only %}{% block card_body %}This is a card with a CTA {{ include('sdc_test:my-cta', { text: content.heading, href: 'https://www.example.org', target: '_blank' }, with_context = false) }}{% endblock %}{% endembed %}", + '#template' => "{% embed 'sdc_theme_test:my-card' with { variant: 'horizontal', header: 'Card header', content: content } only %}{% block card_body %}This is a card with a CTA {{ include('sdc_test:my-cta', { text: content.heading, href: 'https://www.example.org', target: '_blank' }, with_context = false) }}{% endblock %}{% endembed %}", ]; $crawler = $this->renderComponentRenderArray($build); $this->assertNotEmpty($crawler->filter('#sdc-wrapper [data-component-id="sdc_theme_test:my-card"] h2.component--my-card__header:contains("Card header")')); @@ -353,6 +353,36 @@ class ComponentRenderTest extends ComponentKernelTestBase { } /** + * Ensure that components variants render. + */ + public function testVariants(): void { + $build = [ + '#type' => 'component', + '#component' => 'sdc_test:my-cta', + '#variant' => 'primary', + '#props' => [ + 'text' => 'Test link', + ], + ]; + $crawler = $this->renderComponentRenderArray($build); + $this->assertNotEmpty($crawler->filter('#sdc-wrapper a[data-component-id="sdc_test:my-cta"][data-component-variant="primary"][class*="my-cta-primary"]')); + + // If there were an existing prop named variant, we don't override that for BC reasons. + $build = [ + '#type' => 'component', + '#component' => 'sdc_test:my-cta-with-variant-prop', + '#variant' => 'tertiary', + '#props' => [ + 'text' => 'Test link', + 'variant' => 'secondary', + ], + ]; + $crawler = $this->renderComponentRenderArray($build); + $this->assertEmpty($crawler->filter('#sdc-wrapper a[data-component-id="sdc_test:my-cta-with-variant-prop"][data-component-variant="tertiary"]')); + $this->assertNotEmpty($crawler->filter('#sdc-wrapper a[data-component-id="sdc_test:my-cta-with-variant-prop"][data-component-variant="secondary"]')); + } + + /** * Ensures some key aspects of the plugin definition are correctly computed. * * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException diff --git a/core/tests/Drupal/KernelTests/Core/Config/ConfigEntityValidationTestBase.php b/core/tests/Drupal/KernelTests/Core/Config/ConfigEntityValidationTestBase.php index ad556d07e0ac..cb0b3086b141 100644 --- a/core/tests/Drupal/KernelTests/Core/Config/ConfigEntityValidationTestBase.php +++ b/core/tests/Drupal/KernelTests/Core/Config/ConfigEntityValidationTestBase.php @@ -260,7 +260,7 @@ abstract class ConfigEntityValidationTestBase extends KernelTestBase { ], [ 'dependencies.module.0' => [ - 'This value is not valid.', + 'This value is not a valid extension name.', "Module 'invalid-module-name' is not installed.", ], ], @@ -290,7 +290,7 @@ abstract class ConfigEntityValidationTestBase extends KernelTestBase { ], [ 'dependencies.theme.0' => [ - 'This value is not valid.', + 'This value is not a valid extension name.', "Theme 'invalid-theme-name' is not installed.", ], ], diff --git a/core/tests/Drupal/KernelTests/Core/Extension/ExtensionNameConstraintTest.php b/core/tests/Drupal/KernelTests/Core/Extension/ExtensionNameConstraintTest.php index 568a3d3ca4f5..4aaff77dbaa2 100644 --- a/core/tests/Drupal/KernelTests/Core/Extension/ExtensionNameConstraintTest.php +++ b/core/tests/Drupal/KernelTests/Core/Extension/ExtensionNameConstraintTest.php @@ -39,7 +39,7 @@ class ExtensionNameConstraintTest extends KernelTestBase { $data->setValue('invalid-name'); $violations = $data->validate(); $this->assertCount(1, $violations); - $this->assertSame('This value is not valid.', (string) $violations->get(0)->getMessage()); + $this->assertSame('This value is not a valid extension name.', (string) $violations->get(0)->getMessage()); } } diff --git a/core/tests/Drupal/Tests/Core/DefaultContent/FinderTest.php b/core/tests/Drupal/Tests/Core/DefaultContent/FinderTest.php index 6abfb5b67331..ad6f98c3cf78 100644 --- a/core/tests/Drupal/Tests/Core/DefaultContent/FinderTest.php +++ b/core/tests/Drupal/Tests/Core/DefaultContent/FinderTest.php @@ -4,7 +4,6 @@ declare(strict_types=1); namespace Drupal\Tests\Core\DefaultContent; -use Drupal\Component\FileSystem\FileSystem; use Drupal\Core\DefaultContent\Finder; use Drupal\Core\DefaultContent\ImportException; use Drupal\Tests\UnitTestCase; @@ -38,14 +37,9 @@ class FinderTest extends UnitTestCase { * Tests that files without UUIDs will raise an exception. */ public function testExceptionIfNoUuid(): void { - $dir = FileSystem::getOsTemporaryDirectory(); - $this->assertIsString($dir); - /** @var string $dir */ - file_put_contents($dir . '/no-uuid.yml', '_meta: {}'); - $this->expectException(ImportException::class); - $this->expectExceptionMessage("$dir/no-uuid.yml does not have a UUID."); - new Finder($dir); + $this->expectExceptionMessageMatches("#/no-uuid\.yml does not have a UUID\.$#"); + new Finder(__DIR__ . '/../../../../fixtures/default_content_broken'); } } diff --git a/core/tests/Drupal/Tests/Core/PageCache/DenyNoCacheRoutesTest.php b/core/tests/Drupal/Tests/Core/PageCache/DenyNoCacheRoutesTest.php new file mode 100644 index 000000000000..4cc4dafcd531 --- /dev/null +++ b/core/tests/Drupal/Tests/Core/PageCache/DenyNoCacheRoutesTest.php @@ -0,0 +1,92 @@ +<?php + +declare(strict_types=1); + +namespace Drupal\Tests\Core\PageCache; + +use Drupal\Core\PageCache\ResponsePolicyInterface; +use Drupal\Core\PageCache\ResponsePolicy\DenyNoCacheRoutes; +use Drupal\Core\Routing\RouteMatchInterface; +use Drupal\Tests\UnitTestCase; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\Routing\Route; + +/** + * @coversDefaultClass \Drupal\Core\PageCache\ResponsePolicy\DenyNoCacheRoutes + * @group PageCache + * @group Route + */ +class DenyNoCacheRoutesTest extends UnitTestCase { + + /** + * The response policy under test. + * + * @var \Drupal\Core\PageCache\ResponsePolicy\DenyNoCacheRoutes + */ + protected $policy; + + /** + * A request object. + * + * @var \Symfony\Component\HttpFoundation\Request + */ + protected $request; + + /** + * A response object. + * + * @var \Symfony\Component\HttpFoundation\Response + */ + protected $response; + + /** + * The current route match. + * + * @var \Drupal\Core\Routing\RouteMatch|\PHPUnit\Framework\MockObject\MockObject + */ + protected $routeMatch; + + /** + * {@inheritdoc} + */ + protected function setUp(): void { + parent::setUp(); + + $this->routeMatch = $this->createMock(RouteMatchInterface::class); + $this->policy = new DenyNoCacheRoutes($this->routeMatch); + $this->response = new Response(); + $this->request = new Request(); + } + + /** + * Asserts that caching is denied on the node preview route. + * + * @dataProvider providerDenyNoCacheRoutesPolicy + * @covers ::check + */ + public function testDenyNoCacheRoutesPolicy($expected_result, ?Route $route): void { + $this->routeMatch->expects($this->once()) + ->method('getRouteObject') + ->willReturn($route); + + $actual_result = $this->policy->check($this->response, $this->request); + $this->assertSame($expected_result, $actual_result); + } + + /** + * Provides data and expected results for the test method. + * + * @return array + * Data and expected results. + */ + public static function providerDenyNoCacheRoutesPolicy(): array { + $no_cache_route = new Route('', [], [], ['no_cache' => TRUE]); + return [ + [ResponsePolicyInterface::DENY, $no_cache_route], + [NULL, new Route('')], + [NULL, NULL], + ]; + } + +} diff --git a/core/tests/Drupal/Tests/Core/Theme/Component/ComponentValidatorTest.php b/core/tests/Drupal/Tests/Core/Theme/Component/ComponentValidatorTest.php index df03332cf4b1..84d87390552d 100644 --- a/core/tests/Drupal/Tests/Core/Theme/Component/ComponentValidatorTest.php +++ b/core/tests/Drupal/Tests/Core/Theme/Component/ComponentValidatorTest.php @@ -123,6 +123,50 @@ class ComponentValidatorTest extends TestCase { ], ]; yield 'invalid slot (type)' => [$cta_with_invalid_slot_type]; + + $cta_with_invalid_variant_title_type = $valid_cta; + $cta_with_invalid_variant_title_type['variants'] = [ + 'valid_variant' => [ + 'title' => 'Valid variant', + 'description' => 'Valid variant description', + ], + 'invalid_variant' => [ + 'title' => [ + 'hello' => 'Invalid variant', + 'world' => 'Invalid variant', + ], + 'description' => 'Title must be string', + ], + ]; + yield 'invalid variant title (type)' => [$cta_with_invalid_variant_title_type]; + + $cta_with_missing_variant_title_type = $valid_cta; + $cta_with_missing_variant_title_type['variants'] = [ + 'valid_variant' => [ + 'title' => 'Valid variant', + 'description' => 'Valid variant description', + ], + 'invalid_variant' => [ + 'description' => 'Title is required', + ], + ]; + yield 'invalid variant title (missing title)' => [$cta_with_missing_variant_title_type]; + + $cta_with_invalid_variant_description_type = $valid_cta; + $cta_with_invalid_variant_description_type['variants'] = [ + 'valid_variant' => [ + 'title' => 'Valid variant', + 'description' => 'Valid variant description', + ], + 'invalid_variant' => [ + 'title' => 'Invalid variant', + 'description' => [ + 'this' => 'Description must be', + 'that' => 'a string', + ], + ], + ]; + yield 'invalid variant description (type)' => [$cta_with_invalid_variant_description_type]; } /** diff --git a/core/tests/fixtures/default_content_broken/no-uuid.yml b/core/tests/fixtures/default_content_broken/no-uuid.yml new file mode 100644 index 000000000000..f95a15c6463a --- /dev/null +++ b/core/tests/fixtures/default_content_broken/no-uuid.yml @@ -0,0 +1,23 @@ +_meta: + version: '1.0' + entity_type: block_content + bundle: basic + default_langcode: en +default: + status: + - + value: true + info: + - + value: 'Useful Info' + reusable: + - + value: true + revision_translation_affected: + - + value: true + body: + - + value: "I'd love to put some useful info here." + format: plain_text + summary: '' |