Issue #3406024 by neclimdul, mondrake, Spokje, smustgrave: DependencySerializationTrait depends on removed __PHPUNIT_BOOTSTRAP global

merge-requests/5836/merge
Dave Long 2024-03-02 22:14:05 +00:00
parent 563e61adb2
commit 24f8f3c961
No known key found for this signature in database
GPG Key ID: ED52AE211E142771
3 changed files with 57 additions and 46 deletions

View File

@ -72,29 +72,13 @@ trait DependencySerializationTrait {
*/
#[\ReturnTypeWillChange]
public function __wakeup() {
// Tests in isolation potentially unserialize in the parent process.
$phpunit_bootstrap = isset($GLOBALS['__PHPUNIT_BOOTSTRAP']);
if ($phpunit_bootstrap && !\Drupal::hasContainer()) {
return;
}
$container = \Drupal::getContainer();
foreach ($this->_serviceIds as $key => $service_id) {
// In rare cases, when test data is serialized in the parent process,
// there is a service container but it doesn't contain all expected
// services. To avoid fatal errors during the wrap-up of failing tests, we
// check for this case, too.
if ($phpunit_bootstrap && !$container->has($service_id)) {
continue;
}
$this->$key = $container->get($service_id);
}
$this->_serviceIds = [];
// In rare cases, when test data is serialized in the parent process, there
// is a service container but it doesn't contain all expected services. To
// avoid fatal errors during the wrap-up of failing tests, we check for this
// case, too.
if ($this->_entityStorages && (!$phpunit_bootstrap || $container->has('entity_type.manager'))) {
if ($this->_entityStorages) {
/** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
$entity_type_manager = $container->get('entity_type.manager');
foreach ($this->_entityStorages as $key => $entity_type_id) {

View File

@ -0,0 +1,51 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\views_ui\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Drupal\views\Entity\View;
use Drupal\views\ViewExecutable;
use Drupal\views_ui\ViewUI;
/**
* @group views_ui
*/
class ViewsUiObjectTest extends KernelTestBase {
/**
* Modules to enable.
*
* @var array
*/
protected static $modules = ['views', 'views_ui'];
/**
* Tests serialization of the ViewUI object.
*/
public function testSerialization(): void {
$storage = new View([], 'view');
$executable = $this->getMockBuilder(ViewExecutable::class)
->disableOriginalConstructor()
->setConstructorArgs([$storage])
->getMock();
$storage->set('executable', $executable);
$view_ui = new ViewUI($storage);
// Make sure the executable is returned before serializing.
$this->assertInstanceOf(ViewExecutable::class, $view_ui->getExecutable());
$serialized = serialize($view_ui);
// Make sure the ViewExecutable class is not found in the serialized string.
$this->assertStringNotContainsString('"Drupal\views\ViewExecutable"', $serialized);
$unserialized = unserialize($serialized);
$this->assertInstanceOf(ViewUI::class, $unserialized);
// Ensure serialization magic repopulated the object with the executable.
$this->assertInstanceOf(ViewExecutable::class, $unserialized->getExecutable());
}
}

View File

@ -7,7 +7,6 @@ namespace Drupal\Tests\views_ui\Unit;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\TempStore\Lock;
use Drupal\Tests\UnitTestCase;
use Drupal\views\Entity\View;
use Drupal\views_ui\ViewUI;
use Symfony\Component\DependencyInjection\ContainerBuilder;
@ -20,7 +19,7 @@ class ViewUIObjectTest extends UnitTestCase {
/**
* Tests entity method decoration.
*/
public function testEntityDecoration() {
public function testEntityDecoration(): void {
$method_args = [];
$method_args['setOriginalId'] = [12];
$method_args['setStatus'] = [TRUE];
@ -74,8 +73,10 @@ class ViewUIObjectTest extends UnitTestCase {
/**
* Tests the isLocked method.
*
* @runInSeparateProcess
*/
public function testIsLocked() {
public function testIsLocked(): void {
$storage = $this->getMockBuilder('Drupal\views\Entity\View')
->setConstructorArgs([[], 'view'])
->getMock();
@ -108,33 +109,8 @@ class ViewUIObjectTest extends UnitTestCase {
$view_ui->setLock($lock);
$this->assertFalse($view_ui->isLocked());
$view_ui->unsetLock(NULL);
$view_ui->unsetLock();
$this->assertFalse($view_ui->isLocked());
}
/**
* Tests serialization of the ViewUI object.
*/
public function testSerialization() {
$storage = new View([], 'view');
$executable = $this->getMockBuilder('Drupal\views\ViewExecutable')
->disableOriginalConstructor()
->setConstructorArgs([$storage])
->getMock();
$storage->set('executable', $executable);
$view_ui = new ViewUI($storage);
// Make sure the executable is returned before serializing.
$this->assertInstanceOf('Drupal\views\ViewExecutable', $view_ui->getExecutable());
$serialized = serialize($view_ui);
// Make sure the ViewExecutable class is not found in the serialized string.
$this->assertStringNotContainsString('"Drupal\views\ViewExecutable"', $serialized);
$unserialized = unserialize($serialized);
$this->assertInstanceOf('Drupal\views_ui\ViewUI', $unserialized);
}
}