diff --git a/core/modules/system/tests/modules/test_page_test/src/Controller/TestPageTestController.php b/core/modules/system/tests/modules/test_page_test/src/Controller/TestPageTestController.php index 02ea524b09..423d144c4f 100644 --- a/core/modules/system/tests/modules/test_page_test/src/Controller/TestPageTestController.php +++ b/core/modules/system/tests/modules/test_page_test/src/Controller/TestPageTestController.php @@ -2,6 +2,8 @@ namespace Drupal\test_page_test\Controller; +use Drupal\user\Entity\Role; + /** * Controller routines for test_page_test routes. */ @@ -23,4 +25,16 @@ public function testPage() { ]; } + /** + * Returns a test page and with the call to the dump() method. + */ + public function testPageVarDump() { + $role = Role::create(['id' => 'test_role']); + dump($role); + return [ + '#title' => t('Test page with var dump'), + '#markup' => t('Test page text.'), + ]; + } + } diff --git a/core/modules/system/tests/modules/test_page_test/test_page_test.routing.yml b/core/modules/system/tests/modules/test_page_test/test_page_test.routing.yml index 43b6cf54c1..f3744df5ee 100644 --- a/core/modules/system/tests/modules/test_page_test/test_page_test.routing.yml +++ b/core/modules/system/tests/modules/test_page_test/test_page_test.routing.yml @@ -144,3 +144,11 @@ test_page_test.deprecations: _controller: '\Drupal\test_page_test\Controller\Test::deprecations' requirements: _access: 'TRUE' + +test_page_test.test_page_var_dump: + path: '/test-page-var-dump' + defaults: + _title: 'Test front page with var dump' + _controller: '\Drupal\test_page_test\Controller\TestPageTestController::testPageVarDump' + requirements: + _access: 'TRUE' diff --git a/core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php b/core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php index 4be2efc648..263cb29228 100644 --- a/core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php +++ b/core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php @@ -917,4 +917,39 @@ public function testDeprecationHeaders() { $this->assertCount(1, $test_deprecation_messages); } + /** + * Tests the dump() method provided by the Symfony component var-dump. + */ + public function testVarDump() { + // Visit a Drupal page with call to the dump() method. + $body = $this->drupalGet('test-page-var-dump'); + $this->assertSession()->statusCodeEquals(200); + + $this->assertStringContainsString('', $body); + $this->assertStringContainsString(' #id: "test_role"', $body); + $this->assertStringContainsString(' #label: null', $body); + $this->assertStringContainsString(' #weight: null', $body); + $this->assertStringContainsString(' #permissions: []', $body); + $this->assertStringContainsString(' #is_admin: null', $body); + $this->assertStringContainsString(' #originalId: "test_role"', $body); + $this->assertStringContainsString(' #status: true', $body); + $this->assertStringContainsString(' #uuid: "', $body); + $this->assertStringContainsString(' -isUninstalling: false', $body); + $this->assertStringContainsString(' #langcode: "en"', $body); + $this->assertStringContainsString(' #third_party_settings: []', $body); + $this->assertStringContainsString(' #_core: []', $body); + $this->assertStringContainsString(' #trustedData: false', $body); + $this->assertStringContainsString(' #entityTypeId: "user_role"', $body); + $this->assertStringContainsString(' #enforceIsNew: true', $body); + $this->assertStringContainsString(' #typedData: null', $body); + $this->assertStringContainsString(' #cacheContexts: []', $body); + $this->assertStringContainsString(' #cacheTags: []', $body); + $this->assertStringContainsString(' #cacheMaxAge: -1', $body); + $this->assertStringContainsString(' #_serviceIds: []', $body); + $this->assertStringContainsString(' #_entityStorages: []', $body); + $this->assertStringContainsString(' #dependencies: []', $body); + $this->assertStringContainsString(' #isSyncing: false', $body); + $this->assertStringContainsString('}', $body); + } + } diff --git a/core/tests/Drupal/KernelTests/KernelTestBase.php b/core/tests/Drupal/KernelTests/KernelTestBase.php index afe40b747a..c8228546f3 100644 --- a/core/tests/Drupal/KernelTests/KernelTestBase.php +++ b/core/tests/Drupal/KernelTests/KernelTestBase.php @@ -20,6 +20,7 @@ use Drupal\Tests\ConfigTestTrait; use Drupal\Tests\RandomGeneratorTrait; use Drupal\Tests\TestRequirementsTrait; +use Drupal\TestTools\DrupalVarDumperHandler; use Drupal\Tests\Traits\PHPUnit8Warnings; use Drupal\TestTools\Comparator\MarkupInterfaceComparator; use PHPUnit\Framework\Exception; @@ -30,6 +31,7 @@ use org\bovigo\vfs\visitor\vfsStreamPrintVisitor; use Drupal\Core\Routing\RouteObjectInterface; use Symfony\Component\Routing\Route; +use Symfony\Component\VarDumper\VarDumper; /** * Base class for functional integration tests. @@ -221,6 +223,7 @@ abstract class KernelTestBase extends TestCase implements ServiceProviderInterfa */ public static function setUpBeforeClass() { parent::setUpBeforeClass(); + VarDumper::setHandler(DrupalVarDumperHandler::class . '::cliHandler'); // Change the current dir to DRUPAL_ROOT. chdir(static::getDrupalRoot()); diff --git a/core/tests/Drupal/KernelTests/KernelTestBaseTest.php b/core/tests/Drupal/KernelTests/KernelTestBaseTest.php index 8a00387e70..35e1985569 100644 --- a/core/tests/Drupal/KernelTests/KernelTestBaseTest.php +++ b/core/tests/Drupal/KernelTests/KernelTestBaseTest.php @@ -4,6 +4,7 @@ use Drupal\Component\FileCache\FileCacheFactory; use Drupal\Core\Database\Database; +use Drupal\user\Entity\Role; use org\bovigo\vfs\vfsStream; use org\bovigo\vfs\visitor\vfsStreamStructureVisitor; use PHPUnit\Framework\SkippedTestError; @@ -340,4 +341,42 @@ public function testKernelTestBaseInstallSchema() { $this->assertFalse(Database::getConnection()->schema()->tableExists('key_value')); } + /** + * Tests the dump() method provided by the Symfony component var-dump. + */ + public function testVarDump() { + $this->enableModules(['system', 'user']); + $role = Role::create(['id' => 'test_role']); + $uuid = $role->uuid(); + $this->expectOutputRegex('|Drupal\\\\user\\\\Entity\\\\Role|'); + // Assert that `#foo: bar\n` like pattern presents. + $this->expectOutputRegex('|#.+:.+\\\n|'); + $this->expectOutputRegex('|id.+test_role|'); + $this->expectOutputRegex('|label.+null|'); + $this->expectOutputRegex('|weight.+null|'); + $this->expectOutputRegex('|permissions.+\[\]|'); + $this->expectOutputRegex('|is_admin.+null|'); + $this->expectOutputRegex('|originalId.+test_role|'); + $this->expectOutputRegex('|status.+true|'); + $this->expectOutputRegex("|uuid.+$uuid|"); + // Assert that `-foo: bar\n` like pattern presents. + $this->expectOutputRegex('|\-.+:.+\\\n|'); + $this->expectOutputRegex('|isUninstalling.+false|'); + $this->expectOutputRegex('|langcode.+en|'); + $this->expectOutputRegex('|third_party_settings.+\[\]|'); + $this->expectOutputRegex('|_core.+\[\]|'); + $this->expectOutputRegex('|trustedData.+false|'); + $this->expectOutputRegex('|entityTypeId.+user_role|'); + $this->expectOutputRegex('|enforceIsNew.+true|'); + $this->expectOutputRegex('|typedData.+null|'); + $this->expectOutputRegex('|cacheContexts.+\[\]|'); + $this->expectOutputRegex('|cacheTags.+\[\]|'); + $this->expectOutputRegex('|cacheMaxAge.+\-1|'); + $this->expectOutputRegex('|_serviceIds.+\[\]|'); + $this->expectOutputRegex('|_entityStorages.+\[\]|'); + $this->expectOutputRegex('|dependencies.+\[\]|'); + $this->expectOutputRegex('|isSyncing.+false|'); + dump($role); + } + } diff --git a/core/tests/Drupal/TestTools/DrupalVarDumperHandler.php b/core/tests/Drupal/TestTools/DrupalVarDumperHandler.php new file mode 100644 index 0000000000..d38029daa6 --- /dev/null +++ b/core/tests/Drupal/TestTools/DrupalVarDumperHandler.php @@ -0,0 +1,46 @@ +setColors(TRUE); + $dumper->dump( + $cloner->cloneVar($var), + function ($line, $depth, $indent_pad) { + // A negative depth means "end of dump". + if ($depth >= 0) { + // Adds a two spaces indentation to the line. + print str_repeat($indent_pad, $depth) . $line . "\n"; + } + } + ); + } + + /** + * A HTML handler for \Symfony\Component\VarDumper\VarDumper. + */ + public static function htmlHandler($var) { + $cloner = new VarCloner(); + $dumper = new HtmlDumper(); + $dumper->dump($cloner->cloneVar($var)); + } + +} diff --git a/core/tests/Drupal/Tests/BrowserTestBase.php b/core/tests/Drupal/Tests/BrowserTestBase.php index 0aac9ce747..93d922ca70 100644 --- a/core/tests/Drupal/Tests/BrowserTestBase.php +++ b/core/tests/Drupal/Tests/BrowserTestBase.php @@ -19,11 +19,13 @@ use Drupal\Tests\Traits\PHPUnit8Warnings; use Drupal\Tests\user\Traits\UserCreationTrait; use Drupal\TestTools\Comparator\MarkupInterfaceComparator; +use Drupal\TestTools\DrupalVarDumperHandler; use GuzzleHttp\Cookie\CookieJar; use PHPUnit\Framework\TestCase; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Symfony\Component\CssSelector\CssSelectorConverter; +use Symfony\Component\VarDumper\VarDumper; /** * Provides a test case for functional Drupal tests. @@ -212,6 +214,14 @@ abstract class BrowserTestBase extends TestCase { */ protected $originalContainer; + /** + * {@inheritdoc} + */ + public static function setUpBeforeClass() { + parent::setUpBeforeClass(); + VarDumper::setHandler(DrupalVarDumperHandler::class . '::htmlHandler'); + } + /** * Initializes Mink sessions. */