diff options
Diffstat (limited to 'core/tests/Drupal')
20 files changed, 289 insertions, 85 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/KernelTests/Core/Extension/ModuleHandlerTest.php b/core/tests/Drupal/KernelTests/Core/Extension/ModuleHandlerTest.php index 3d5a6c8508ba..8ee28a968fd5 100644 --- a/core/tests/Drupal/KernelTests/Core/Extension/ModuleHandlerTest.php +++ b/core/tests/Drupal/KernelTests/Core/Extension/ModuleHandlerTest.php @@ -36,20 +36,4 @@ class ModuleHandlerTest extends KernelTestBase { $this->assertNotNull(\Drupal::service('module_handler')->getName('module_test')); } - /** - * Tests that resetImplementations clears the invokeMap memory cache. - * - * @covers ::resetImplementations - */ - public function testResetImplementationsClearsInvokeMap(): void { - /** @var \Drupal\Core\Extension\ModuleInstallerInterface $moduleInstaller */ - $moduleInstaller = \Drupal::service('module_installer'); - $moduleInstaller->install(['module_test']); - /** @var \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler */ - $moduleHandler = \Drupal::service('module_handler'); - $this->assertTrue($moduleHandler->hasImplementations('system_info_alter')); - $moduleInstaller->uninstall(['module_test']); - $this->assertFalse($moduleHandler->hasImplementations('system_info_alter')); - } - } diff --git a/core/tests/Drupal/KernelTests/Core/Plugin/DefaultPluginManagerTest.php b/core/tests/Drupal/KernelTests/Core/Plugin/DefaultPluginManagerTest.php index 21370884e107..caf97cb6c0de 100644 --- a/core/tests/Drupal/KernelTests/Core/Plugin/DefaultPluginManagerTest.php +++ b/core/tests/Drupal/KernelTests/Core/Plugin/DefaultPluginManagerTest.php @@ -14,6 +14,7 @@ use org\bovigo\vfs\vfsStream; * Tests the default plugin manager. * * @group Plugin + * @group legacy */ class DefaultPluginManagerTest extends KernelTestBase { @@ -44,6 +45,7 @@ class DefaultPluginManagerTest extends KernelTestBase { // Ensure there is a class with the expected name. We cannot reflect on this // as it triggers a fatal error. $this->assertFileExists($base_directory . '/' . $subdir . '/UsingNonInstalledTraitClass.php'); + $this->expectDeprecation('Using @PluginExample annotation for plugin with ID example_1 is deprecated and is removed from drupal:13.0.0. Use a Drupal\plugin_test\Plugin\Attribute\PluginExample attribute instead. See https://www.drupal.org/node/3395575'); // Annotation only. $manager = new DefaultPluginManager($subdir, $namespaces, $module_handler, NULL, AnnotationPluginExample::class); @@ -99,4 +101,19 @@ class DefaultPluginManagerTest extends KernelTestBase { $this->assertArrayNotHasKey('example_annotation_not_attribute', $definitions); } + /** + * Tests the deprecation message for using only annotations. + */ + public function testDefaultPluginManagerAnnotationsOnly(): void { + $subdir = 'Plugin/plugin_test/custom_annotation'; + $base_directory = $this->root . '/core/modules/system/tests/modules/plugin_test/src'; + $namespaces = new \ArrayObject(['Drupal\plugin_test' => $base_directory]); + $module_handler = $this->container->get('module_handler'); + + $this->expectDeprecation('Not supporting attribute discovery in Drupal\Core\Plugin\DefaultPluginManager is deprecated in drupal:11.2.0 and is removed from drupal:12.0.0. Provide an Attribute class and an Annotation class for BC. See https://www.drupal.org/node/3395582'); + + $manager = new DefaultPluginManager($subdir, $namespaces, $module_handler, NULL, AnnotationPluginExample::class); + $manager->getDefinitions(); + } + } diff --git a/core/tests/Drupal/KernelTests/Core/Test/PhpUnitTestDiscoveryTest.php b/core/tests/Drupal/KernelTests/Core/Test/PhpUnitTestDiscoveryTest.php index 6d7fcaeed9ae..705981f75079 100644 --- a/core/tests/Drupal/KernelTests/Core/Test/PhpUnitTestDiscoveryTest.php +++ b/core/tests/Drupal/KernelTests/Core/Test/PhpUnitTestDiscoveryTest.php @@ -6,6 +6,7 @@ namespace Drupal\KernelTests\Core\Test; use Drupal\Core\Test\TestDiscovery; use Drupal\KernelTests\KernelTestBase; +use Drupal\TestTools\PhpUnitCompatibility\RunnerVersion; use PHPUnit\TextUI\Configuration\Builder; use PHPUnit\TextUI\Configuration\TestSuiteBuilder; use Symfony\Component\Process\Process; @@ -96,14 +97,28 @@ class PhpUnitTestDiscoveryTest extends KernelTestBase { $phpUnitXmlList = new \DOMDocument(); $phpUnitXmlList->loadXML(file_get_contents($this->xmlOutputFile)); $phpUnitClientList = []; + // Try PHPUnit 10 format first. + // @todo remove once PHPUnit 10 is no longer used. foreach ($phpUnitXmlList->getElementsByTagName('testCaseClass') as $node) { $phpUnitClientList[] = $node->getAttribute('name'); } + // If empty, try PHPUnit 11+ format. + if (empty($phpUnitClientList)) { + foreach ($phpUnitXmlList->getElementsByTagName('testClass') as $node) { + $phpUnitClientList[] = $node->getAttribute('name'); + } + } asort($phpUnitClientList); // Check against Drupal's discovery. $this->assertEquals(implode("\n", $phpUnitClientList), implode("\n", $internalList), self::TEST_LIST_MISMATCH_MESSAGE); + // @todo once PHPUnit 10 is no longer used re-enable the rest of the test. + // @see https://www.drupal.org/project/drupal/issues/3497116 + if (RunnerVersion::getMajor() >= 11) { + $this->markTestIncomplete('On PHPUnit 11+ the test triggers warnings due to phpunit.xml setup. Re-enable in https://www.drupal.org/project/drupal/issues/3497116.'); + } + // PHPUnit's test discovery - via API. $phpUnitConfiguration = (new Builder())->build(['--configuration', 'core']); $phpUnitTestSuite = (new TestSuiteBuilder())->build($phpUnitConfiguration); diff --git a/core/tests/Drupal/TestTools/Extension/DeprecationBridge/DeprecationHandler.php b/core/tests/Drupal/TestTools/Extension/DeprecationBridge/DeprecationHandler.php index c176980bae27..f66ef7c8c947 100644 --- a/core/tests/Drupal/TestTools/Extension/DeprecationBridge/DeprecationHandler.php +++ b/core/tests/Drupal/TestTools/Extension/DeprecationBridge/DeprecationHandler.php @@ -76,6 +76,11 @@ final class DeprecationHandler { $environmentVariable = "ignoreFile=$deprecationIgnoreFilename"; } parse_str($environmentVariable, $configuration); + + $environmentVariable = getenv('PHPUNIT_FAIL_ON_PHPUNIT_DEPRECATION'); + $phpUnitDeprecationVariable = $environmentVariable !== FALSE ? $environmentVariable : TRUE; + $configuration['failOnPhpunitDeprecation'] = filter_var($phpUnitDeprecationVariable, \FILTER_VALIDATE_BOOLEAN); + return $configuration; } diff --git a/core/tests/Drupal/TestTools/PhpUnitCompatibility/PhpUnit11/TestCompatibilityTrait.php b/core/tests/Drupal/TestTools/PhpUnitCompatibility/PhpUnit11/TestCompatibilityTrait.php new file mode 100644 index 000000000000..84638f9f0f57 --- /dev/null +++ b/core/tests/Drupal/TestTools/PhpUnitCompatibility/PhpUnit11/TestCompatibilityTrait.php @@ -0,0 +1,13 @@ +<?php + +declare(strict_types=1); + +namespace Drupal\TestTools\PhpUnitCompatibility\PhpUnit11; + +/** + * Drupal's forward compatibility layer with multiple versions of PHPUnit. + * + * @internal + */ +trait TestCompatibilityTrait { +} diff --git a/core/tests/Drupal/Tests/Component/Datetime/DateTimePlusTest.php b/core/tests/Drupal/Tests/Component/Datetime/DateTimePlusTest.php index 8dd4d8fb5948..ae46c3cc5a56 100644 --- a/core/tests/Drupal/Tests/Component/Datetime/DateTimePlusTest.php +++ b/core/tests/Drupal/Tests/Component/Datetime/DateTimePlusTest.php @@ -671,16 +671,16 @@ class DateTimePlusTest extends TestCase { // There should be a 19 hour time interval between // new years in Sydney and new years in LA in year 2000. [ - 'input2' => DateTimePlus::createFromFormat('Y-m-d H:i:s', '2000-01-01 00:00:00', new \DateTimeZone('Australia/Sydney')), - 'input1' => DateTimePlus::createFromFormat('Y-m-d H:i:s', '2000-01-01 00:00:00', new \DateTimeZone('America/Los_Angeles')), + 'input1' => DateTimePlus::createFromFormat('Y-m-d H:i:s', '2000-01-01 00:00:00', new \DateTimeZone('Australia/Sydney')), + 'input2' => DateTimePlus::createFromFormat('Y-m-d H:i:s', '2000-01-01 00:00:00', new \DateTimeZone('America/Los_Angeles')), 'absolute' => FALSE, 'expected' => $positive_19_hours, ], // In 1970 Sydney did not observe daylight savings time // So there is only an 18 hour time interval. [ - 'input2' => DateTimePlus::createFromFormat('Y-m-d H:i:s', '1970-01-01 00:00:00', new \DateTimeZone('Australia/Sydney')), - 'input1' => DateTimePlus::createFromFormat('Y-m-d H:i:s', '1970-01-01 00:00:00', new \DateTimeZone('America/Los_Angeles')), + 'input1' => DateTimePlus::createFromFormat('Y-m-d H:i:s', '1970-01-01 00:00:00', new \DateTimeZone('Australia/Sydney')), + 'input2' => DateTimePlus::createFromFormat('Y-m-d H:i:s', '1970-01-01 00:00:00', new \DateTimeZone('America/Los_Angeles')), 'absolute' => FALSE, 'expected' => $positive_18_hours, ], diff --git a/core/tests/Drupal/Tests/Component/Utility/HtmlTest.php b/core/tests/Drupal/Tests/Component/Utility/HtmlTest.php index b2c459e06a78..800483fab501 100644 --- a/core/tests/Drupal/Tests/Component/Utility/HtmlTest.php +++ b/core/tests/Drupal/Tests/Component/Utility/HtmlTest.php @@ -65,6 +65,7 @@ class HtmlTest extends TestCase { $id1 = 'abcdefghijklmnopqrstuvwxyz_ABCDEFGHIJKLMNOPQRSTUVWXYZ-0123456789'; $id2 = '¡¢£¤¥'; $id3 = 'css__identifier__with__double__underscores'; + $id4 = "\x80\x81"; return [ // Verify that no valid ASCII characters are stripped from the identifier. [$id1, $id1, []], @@ -73,6 +74,8 @@ class HtmlTest extends TestCase { [$id2, $id2, []], // Verify that double underscores are not stripped from the identifier. [$id3, $id3], + // Confirm that NULL identifier does not trigger PHP 8.1 deprecation message. + ['', $id4], // Verify that invalid characters (including non-breaking space) are // stripped from the identifier. ['invalid_identifier', 'invalid_ !"#$%&\'()*+,./:;<=>?@[\\]^`{|}~ identifier', []], diff --git a/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityStorageTest.php b/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityStorageTest.php index 2b4d2990d52e..76f5cc118ae6 100644 --- a/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityStorageTest.php +++ b/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityStorageTest.php @@ -172,25 +172,17 @@ class ConfigEntityStorageTest extends UnitTestCase { */ public function testCreateWithPredefinedUuid(): void { $this->cacheTagsInvalidator->invalidateTags(Argument::cetera())->shouldNotBeCalled(); - - $entity = $this->getMockEntity(); - $entity->set('id', 'foo'); - $entity->set('langcode', 'hu'); - $entity->set('uuid', 'baz'); - $entity->setOriginalId('foo'); - $entity->enforceIsNew(); - - $this->moduleHandler->invokeAll('test_entity_type_create', [$entity]) - ->shouldBeCalled(); - $this->moduleHandler->invokeAll('entity_create', [$entity, 'test_entity_type']) - ->shouldBeCalled(); - $this->uuidService->generate()->shouldNotBeCalled(); $entity = $this->entityStorage->create(['id' => 'foo', 'uuid' => 'baz']); $this->assertInstanceOf(EntityInterface::class, $entity); $this->assertSame('foo', $entity->id()); $this->assertSame('baz', $entity->uuid()); + + $this->moduleHandler->invokeAll('test_entity_type_create', [$entity]) + ->shouldBeCalled(); + $this->moduleHandler->invokeAll('entity_create', [$entity, 'test_entity_type']) + ->shouldBeCalled(); } /** @@ -202,25 +194,18 @@ class ConfigEntityStorageTest extends UnitTestCase { */ public function testCreate() { $this->cacheTagsInvalidator->invalidateTags(Argument::cetera())->shouldNotBeCalled(); + $this->uuidService->generate()->willReturn('bar'); - $entity = $this->getMockEntity(); - $entity->set('id', 'foo'); - $entity->set('langcode', 'hu'); - $entity->set('uuid', 'bar'); - $entity->setOriginalId('foo'); - $entity->enforceIsNew(); + $entity = $this->entityStorage->create(['id' => 'foo']); + $this->assertInstanceOf(EntityInterface::class, $entity); + $this->assertSame('foo', $entity->id()); + $this->assertSame('bar', $entity->uuid()); $this->moduleHandler->invokeAll('test_entity_type_create', [$entity]) ->shouldBeCalled(); $this->moduleHandler->invokeAll('entity_create', [$entity, 'test_entity_type']) ->shouldBeCalled(); - $this->uuidService->generate()->willReturn('bar'); - - $entity = $this->entityStorage->create(['id' => 'foo']); - $this->assertInstanceOf(EntityInterface::class, $entity); - $this->assertSame('foo', $entity->id()); - $this->assertSame('bar', $entity->uuid()); return $entity; } diff --git a/core/tests/Drupal/Tests/Core/Datetime/DrupalDateTimeTest.php b/core/tests/Drupal/Tests/Core/Datetime/DrupalDateTimeTest.php index e1ec84395f9d..40b94bd046bf 100644 --- a/core/tests/Drupal/Tests/Core/Datetime/DrupalDateTimeTest.php +++ b/core/tests/Drupal/Tests/Core/Datetime/DrupalDateTimeTest.php @@ -84,16 +84,16 @@ class DrupalDateTimeTest extends UnitTestCase { // There should be a 19 hour time interval between // new years in Sydney and new years in LA in year 2000. [ - 'input2' => DrupalDateTime::createFromFormat('Y-m-d H:i:s', '2000-01-01 00:00:00', new \DateTimeZone('Australia/Sydney'), $settings), - 'input1' => DrupalDateTime::createFromFormat('Y-m-d H:i:s', '2000-01-01 00:00:00', new \DateTimeZone('America/Los_Angeles'), $settings), + 'input1' => DrupalDateTime::createFromFormat('Y-m-d H:i:s', '2000-01-01 00:00:00', new \DateTimeZone('Australia/Sydney'), $settings), + 'input2' => DrupalDateTime::createFromFormat('Y-m-d H:i:s', '2000-01-01 00:00:00', new \DateTimeZone('America/Los_Angeles'), $settings), 'absolute' => FALSE, 'expected' => $positive_19_hours, ], // In 1970 Sydney did not observe daylight savings time // So there is only an 18 hour time interval. [ - 'input2' => DrupalDateTime::createFromFormat('Y-m-d H:i:s', '1970-01-01 00:00:00', new \DateTimeZone('Australia/Sydney'), $settings), - 'input1' => DrupalDateTime::createFromFormat('Y-m-d H:i:s', '1970-01-01 00:00:00', new \DateTimeZone('America/Los_Angeles'), $settings), + 'input1' => DrupalDateTime::createFromFormat('Y-m-d H:i:s', '1970-01-01 00:00:00', new \DateTimeZone('Australia/Sydney'), $settings), + 'input2' => DrupalDateTime::createFromFormat('Y-m-d H:i:s', '1970-01-01 00:00:00', new \DateTimeZone('America/Los_Angeles'), $settings), 'absolute' => FALSE, 'expected' => $positive_18_hours, ], 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/Entity/KeyValueStore/KeyValueEntityStorageTest.php b/core/tests/Drupal/Tests/Core/Entity/KeyValueStore/KeyValueEntityStorageTest.php index f57a80d1393c..2381b64b83a5 100644 --- a/core/tests/Drupal/Tests/Core/Entity/KeyValueStore/KeyValueEntityStorageTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/KeyValueStore/KeyValueEntityStorageTest.php @@ -206,11 +206,8 @@ class KeyValueEntityStorageTest extends UnitTestCase { /** * @covers ::create * @covers ::doCreate - * - * @return \Drupal\Core\Entity\EntityInterface - * The newly created entity instance with the specified ID and generated UUID. */ - public function testCreate() { + public function testCreate(): void { $entity = $this->getMockEntity(EntityBaseTest::class, [], ['toArray']); $this->entityType->expects($this->once()) ->method('getClass') @@ -231,24 +228,18 @@ class KeyValueEntityStorageTest extends UnitTestCase { $this->assertInstanceOf('Drupal\Core\Entity\EntityInterface', $entity); $this->assertSame('foo', $entity->id()); $this->assertSame('bar', $entity->uuid()); - return $entity; } /** * @covers ::save * @covers ::doSave - * - * @param \Drupal\Core\Entity\EntityInterface $entity - * The entity. - * - * @return \Drupal\Core\Entity\EntityInterface - * The saved entity instance after insertion. - * - * @depends testCreate */ - public function testSaveInsert(EntityInterface $entity) { + public function testSaveInsert(): EntityInterface&MockObject { $this->setUpKeyValueEntityStorage(); + $entity = $this->getMockEntity(EntityBaseTest::class, [['id' => 'foo']], ['toArray']); + $entity->enforceIsNew(); + $expected = ['id' => 'foo']; $this->keyValueStore->expects($this->exactly(2)) ->method('has') @@ -285,12 +276,9 @@ class KeyValueEntityStorageTest extends UnitTestCase { * @param \Drupal\Core\Entity\EntityInterface $entity * The entity. * - * @return \Drupal\Core\Entity\EntityInterface - * The updated entity instance after saving. - * * @depends testSaveInsert */ - public function testSaveUpdate(EntityInterface $entity) { + public function testSaveUpdate(EntityInterface $entity): void { $this->entityType->expects($this->once()) ->method('getClass') ->willReturn(get_class($entity)); @@ -320,7 +308,6 @@ class KeyValueEntityStorageTest extends UnitTestCase { ->with('foo', $expected); $return = $this->entityStorage->save($entity); $this->assertSame(SAVED_UPDATED, $return); - return $entity; } /** diff --git a/core/tests/Drupal/Tests/Core/Entity/Sql/SqlContentEntityStorageTest.php b/core/tests/Drupal/Tests/Core/Entity/Sql/SqlContentEntityStorageTest.php index 37cede409b88..5653e8a356b0 100644 --- a/core/tests/Drupal/Tests/Core/Entity/Sql/SqlContentEntityStorageTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/Sql/SqlContentEntityStorageTest.php @@ -1105,12 +1105,7 @@ class SqlContentEntityStorageTest extends UnitTestCase { $this->setUpEntityStorage(); $entity = $this->entityStorage->create(); - $entity->expects($this->atLeastOnce()) - ->method('id') - ->willReturn('foo'); - $this->assertInstanceOf(EntityInterface::class, $entity); - $this->assertSame('foo', $entity->id()); $this->assertTrue($entity->isNew()); } diff --git a/core/tests/Drupal/Tests/Core/Layout/LayoutPluginManagerTest.php b/core/tests/Drupal/Tests/Core/Layout/LayoutPluginManagerTest.php index 756f562508a6..d662dca53782 100644 --- a/core/tests/Drupal/Tests/Core/Layout/LayoutPluginManagerTest.php +++ b/core/tests/Drupal/Tests/Core/Layout/LayoutPluginManagerTest.php @@ -27,6 +27,7 @@ use Prophecy\Argument; /** * @coversDefaultClass \Drupal\Core\Layout\LayoutPluginManager * @group Layout + * @group legacy */ class LayoutPluginManagerTest extends UnitTestCase { @@ -108,6 +109,8 @@ class LayoutPluginManagerTest extends UnitTestCase { $class_loader->addPsr4("Drupal\\Core\\", vfsStream::url("root/core/lib/Drupal/Core")); $class_loader->register(TRUE); $this->layoutPluginManager = new LayoutPluginManager($namespaces, $this->cacheBackend->reveal(), $this->moduleHandler->reveal(), $this->themeHandler->reveal()); + + $this->expectDeprecation('Using @Layout annotation for plugin with ID plugin_provided_by_annotation_layout is deprecated and is removed from drupal:13.0.0. Use a Drupal\Core\Layout\Attribute\Layout attribute instead. See https://www.drupal.org/node/3395575'); } /** 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]; } /** |