summaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorNathaniel Catchpole2019-01-07 11:21:57 (GMT)
committerNathaniel Catchpole2019-01-07 11:21:57 (GMT)
commit236623f1aa60aa419ee86a87c8af022b6eac3c5c (patch)
tree893086ae41881ada091b0ed7b9ea02d68fde37e0 /core
parent6cd36113bf64d76b3a126234cf3bb058103a5a4f (diff)
Issue #2701829 by alexpott, andypost, Soul88, Graber, Eduardo Morales, dawehner, pingwin4eg, catch, Berdir, jibran, httang12: Extension objects should not implement \Serializable
Diffstat (limited to 'core')
-rw-r--r--core/lib/Drupal/Core/Extension/Extension.php51
-rw-r--r--core/modules/system/tests/themes/test_subseven/test_subseven.info.yml6
-rw-r--r--core/modules/views/tests/modules/user_batch_action_test/config/install/system.action.user_batch_action_test_action.yml10
-rw-r--r--core/modules/views/tests/modules/user_batch_action_test/config/schema/user_batch_action_test.schema.yml3
-rw-r--r--core/modules/views/tests/modules/user_batch_action_test/src/Plugin/Action/BatchUserAction.php92
-rw-r--r--core/modules/views/tests/modules/user_batch_action_test/user_batch_action_test.info.yml9
-rw-r--r--core/modules/views/tests/src/Functional/UserBatchActionTest.php44
-rw-r--r--core/tests/Drupal/Tests/Core/Extension/ExtensionSerializationTest.php32
8 files changed, 214 insertions, 33 deletions
diff --git a/core/lib/Drupal/Core/Extension/Extension.php b/core/lib/Drupal/Core/Extension/Extension.php
index 7cb6b56..b8cdd42 100644
--- a/core/lib/Drupal/Core/Extension/Extension.php
+++ b/core/lib/Drupal/Core/Extension/Extension.php
@@ -4,8 +4,13 @@ namespace Drupal\Core\Extension;
/**
* Defines an extension (file) object.
+ *
+ * This class does not implement the Serializable interface since problems
+ * occurred when using the serialize method.
+ *
+ * @see https://bugs.php.net/bug.php?id=66052
*/
-class Extension implements \Serializable {
+class Extension {
/**
* The type of the extension (e.g., 'module').
@@ -156,47 +161,27 @@ class Extension implements \Serializable {
}
/**
- * Implements Serializable::serialize().
+ * Magic method implementation to serialize the extension object.
*
- * Serializes the Extension object in the most optimized way.
+ * @return array
+ * The names of all variables that should be serialized.
*/
- public function serialize() {
+ public function __sleep() {
+ // @todo \Drupal\Core\Extension\ThemeExtensionList is adding custom
+ // properties to the Extension object.
+ $properties = get_object_vars($this);
// Don't serialize the app root, since this could change if the install is
- // moved.
- $data = [
- 'type' => $this->type,
- 'pathname' => $this->pathname,
- 'filename' => $this->filename,
- ];
-
- // @todo ThemeHandler::listInfo(), ThemeHandler::rebuildThemeData(), and
- // system_list() are adding custom properties to the Extension object.
- $info = new \ReflectionObject($this);
- foreach ($info->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) {
- $data[$property->getName()] = $property->getValue($this);
- }
-
- return serialize($data);
+ // moved. Don't serialize splFileInfo because it can not be.
+ unset($properties['splFileInfo'], $properties['root']);
+ return array_keys($properties);
}
/**
- * {@inheritdoc}
+ * Magic method implementation to unserialize the extension object.
*/
- public function unserialize($data) {
- $data = unserialize($data);
+ public function __wakeup() {
// Get the app root from the container.
$this->root = DRUPAL_ROOT;
- $this->type = $data['type'];
- $this->pathname = $data['pathname'];
- $this->filename = $data['filename'];
-
- // @todo ThemeHandler::listInfo(), ThemeHandler::rebuildThemeData(), and
- // system_list() are adding custom properties to the Extension object.
- foreach ($data as $property => $value) {
- if (!isset($this->$property)) {
- $this->$property = $value;
- }
- }
}
}
diff --git a/core/modules/system/tests/themes/test_subseven/test_subseven.info.yml b/core/modules/system/tests/themes/test_subseven/test_subseven.info.yml
new file mode 100644
index 0000000..df9ad9c
--- /dev/null
+++ b/core/modules/system/tests/themes/test_subseven/test_subseven.info.yml
@@ -0,0 +1,6 @@
+name: 'Theme test subseven'
+type: theme
+description: 'Test theme which uses seven as the base theme.'
+version: VERSION
+core: 8.x
+base theme: seven
diff --git a/core/modules/views/tests/modules/user_batch_action_test/config/install/system.action.user_batch_action_test_action.yml b/core/modules/views/tests/modules/user_batch_action_test/config/install/system.action.user_batch_action_test_action.yml
new file mode 100644
index 0000000..6b25663
--- /dev/null
+++ b/core/modules/views/tests/modules/user_batch_action_test/config/install/system.action.user_batch_action_test_action.yml
@@ -0,0 +1,10 @@
+langcode: en
+status: true
+dependencies:
+ module:
+ - user
+id: user_batch_action_test_action
+label: 'Process user in batch'
+type: user
+plugin: user_batch_action_test_action
+configuration: { }
diff --git a/core/modules/views/tests/modules/user_batch_action_test/config/schema/user_batch_action_test.schema.yml b/core/modules/views/tests/modules/user_batch_action_test/config/schema/user_batch_action_test.schema.yml
new file mode 100644
index 0000000..0730941
--- /dev/null
+++ b/core/modules/views/tests/modules/user_batch_action_test/config/schema/user_batch_action_test.schema.yml
@@ -0,0 +1,3 @@
+action.configuration.user_batch_action_test_action:
+ type: action_configuration_default
+ label: 'Process user in batch'
diff --git a/core/modules/views/tests/modules/user_batch_action_test/src/Plugin/Action/BatchUserAction.php b/core/modules/views/tests/modules/user_batch_action_test/src/Plugin/Action/BatchUserAction.php
new file mode 100644
index 0000000..87e01bf
--- /dev/null
+++ b/core/modules/views/tests/modules/user_batch_action_test/src/Plugin/Action/BatchUserAction.php
@@ -0,0 +1,92 @@
+<?php
+
+namespace Drupal\user_batch_action_test\Plugin\Action;
+
+use Drupal\Core\Action\ActionBase;
+use Drupal\Core\Entity\ContentEntityInterface;
+use Drupal\Core\Session\AccountInterface;
+
+/**
+ * Provides action that sets batch precessing.
+ *
+ * @Action(
+ * id = "user_batch_action_test_action",
+ * label = @Translation("Process user in batch"),
+ * type = "user",
+ * )
+ */
+class BatchUserAction extends ActionBase {
+
+ /**
+ * {@inheritdoc}
+ */
+ public function executeMultiple(array $entities) {
+ $operations = [];
+
+ foreach ($entities as $entity) {
+ $operations[] = [
+ [get_class($this), 'processBatch'],
+ [
+ [
+ 'entity_type' => $entity->getEntityTypeId(),
+ 'entity_id' => $entity->id(),
+ ],
+ ],
+ ];
+ }
+
+ if ($operations) {
+ $batch = [
+ 'operations' => $operations,
+ 'finished' => [get_class($this), 'finishBatch'],
+ ];
+ batch_set($batch);
+ }
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function execute(ContentEntityInterface $entity = NULL) {
+ $this->executeMultiple([$entity]);
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
+ return TRUE;
+ }
+
+ /**
+ * Processes the batch item.
+ *
+ * @param array $data
+ * Keyed array of data to process.
+ * @param array $context
+ * The batch context.
+ */
+ public static function processBatch($data, &$context) {
+ if (!isset($context['results']['processed'])) {
+ $context['results']['processed'] = 0;
+ $context['results']['theme'] = \Drupal::service('theme.manager')->getActiveTheme(\Drupal::routeMatch())->getName();
+ }
+ $context['results']['processed']++;
+ }
+
+ /**
+ * Finish batch.
+ *
+ * @param bool $success
+ * Indicates whether the batch process was successful.
+ * @param array $results
+ * Results information passed from the processing callback.
+ */
+ public static function finishBatch($success, $results) {
+ \Drupal::messenger()->addMessage(
+ \Drupal::translation()->formatPlural($results['processed'], 'One item has been processed.', '@count items have been processed.')
+ );
+ \Drupal::messenger()->addMessage($results['theme'] . ' theme used');
+ }
+
+}
diff --git a/core/modules/views/tests/modules/user_batch_action_test/user_batch_action_test.info.yml b/core/modules/views/tests/modules/user_batch_action_test/user_batch_action_test.info.yml
new file mode 100644
index 0000000..931dfc2
--- /dev/null
+++ b/core/modules/views/tests/modules/user_batch_action_test/user_batch_action_test.info.yml
@@ -0,0 +1,9 @@
+name: 'User batch action test'
+type: module
+description: 'Support module for user batch action testing.'
+package: Testing
+version: VERSION
+core: 8.x
+dependencies:
+ - views
+ - user
diff --git a/core/modules/views/tests/src/Functional/UserBatchActionTest.php b/core/modules/views/tests/src/Functional/UserBatchActionTest.php
new file mode 100644
index 0000000..60c1d25
--- /dev/null
+++ b/core/modules/views/tests/src/Functional/UserBatchActionTest.php
@@ -0,0 +1,44 @@
+<?php
+
+namespace Drupal\Tests\views\Functional;
+
+use Drupal\Tests\BrowserTestBase;
+
+/**
+ * Tests the views bulk form with batch action.
+ *
+ * @group action
+ * @see \Drupal\action\Plugin\views\field\BulkForm
+ */
+class UserBatchActionTest extends BrowserTestBase {
+
+ /**
+ * Modules to install.
+ *
+ * @var array
+ */
+ public static $modules = ['user', 'user_batch_action_test', 'views'];
+
+ /**
+ * Tests user admin batch.
+ */
+ public function testUserAction() {
+ $themes = ['classy', 'seven', 'bartik', 'test_subseven'];
+ $this->container->get('theme_installer')->install($themes);
+
+ $this->drupalLogin($this->rootUser);
+
+ foreach ($themes as $theme) {
+ $this->config('system.theme')->set('default', $theme)->save();
+ $this->drupalGet('admin/people');
+ $edit = [
+ 'user_bulk_form[0]' => TRUE,
+ 'action' => 'user_batch_action_test_action',
+ ];
+ $this->drupalPostForm(NULL, $edit, t('Apply'));
+ $this->assertSession()->pageTextContains('One item has been processed.');
+ $this->assertSession()->pageTextContains($theme . ' theme used');
+ }
+ }
+
+}
diff --git a/core/tests/Drupal/Tests/Core/Extension/ExtensionSerializationTest.php b/core/tests/Drupal/Tests/Core/Extension/ExtensionSerializationTest.php
new file mode 100644
index 0000000..e8388d6
--- /dev/null
+++ b/core/tests/Drupal/Tests/Core/Extension/ExtensionSerializationTest.php
@@ -0,0 +1,32 @@
+<?php
+
+namespace Drupal\Tests\Core\Extension;
+
+use Drupal\Tests\UnitTestCase;
+use Drupal\Core\Extension\Extension;
+
+/**
+ * Tests Extension serialization.
+ *
+ * @coversDefaultClass \Drupal\Core\Extension\Extension
+ * @group Extension
+ */
+class ExtensionSerializationTest extends UnitTestCase {
+
+ /**
+ * Tests dynamically assigned public properties kept when serialized.
+ *
+ * @covers ::__sleep
+ * @covers ::__wakeup
+ * @runInSeparateProcess
+ */
+ public function testPublicProperties() {
+ define('DRUPAL_ROOT', '/dummy/app/root');
+ $extension = new Extension('/dummy/app/root', 'module', 'core/modules/system/system.info.yml', 'system.module');
+ // Assign a public property dynamically.
+ $extension->test = 'foo';
+ $extension = unserialize(serialize($extension));
+ $this->assertSame('foo', $extension->test);
+ }
+
+}