Skip to content

Instantly share code, notes, and snippets.

@andypost
Created October 12, 2016 02:04
Show Gist options
  • Save andypost/c9565299aba1d50b58d60a800fc9028a to your computer and use it in GitHub Desktop.
Save andypost/c9565299aba1d50b58d60a800fc9028a to your computer and use it in GitHub Desktop.
diff --git a/core/lib/Drupal.php b/core/lib/Drupal.php
index d124160..005275b 100644
--- a/core/lib/Drupal.php
+++ b/core/lib/Drupal.php
@@ -81,7 +81,7 @@ class Drupal {
/**
* The current system version.
*/
- const VERSION = '8.2.0';
+ const VERSION = '8.2.1';
/**
* Core API compatibility.
diff --git a/core/modules/block/block.post_update.php b/core/modules/block/block.post_update.php
index f208f65..6f14a3d 100644
--- a/core/modules/block/block.post_update.php
+++ b/core/modules/block/block.post_update.php
@@ -77,3 +77,36 @@ function block_post_update_disable_blocks_with_missing_contexts() {
/**
* @} End of "addtogroup updates-8.0.0-beta".
*/
+
+/**
+ * @addtogroup updates-8.2.x
+ * @{
+ */
+
+/**
+ * Fix invalid 'negate' values in block visibility conditions.
+ */
+function block_post_update_fix_negate_in_conditions() {
+ $block_storage = \Drupal::entityTypeManager()->getStorage('block');
+ /** @var \Drupal\block\BlockInterface[] $blocks */
+ $blocks = $block_storage->loadMultiple();
+ foreach ($blocks as $block) {
+ $block_needs_saving = FALSE;
+ // Check each visibility condition for an invalid negate value, and fix it.
+ foreach ($block->getVisibilityConditions() as $condition_id => $condition) {
+ $configuration = $condition->getConfiguration();
+ if (array_key_exists('negate', $configuration) && !is_bool($configuration['negate'])) {
+ $configuration['negate'] = (bool) $configuration['negate'];
+ $condition->setConfiguration($configuration);
+ $block_needs_saving = TRUE;
+ }
+ }
+ if ($block_needs_saving) {
+ $block->save();
+ }
+ }
+}
+
+/**
+ * @} End of "addtogroup updates-8.2.x".
+ */
diff --git a/core/modules/block/src/BlockForm.php b/core/modules/block/src/BlockForm.php
index 2c444da..3f3f92c 100644
--- a/core/modules/block/src/BlockForm.php
+++ b/core/modules/block/src/BlockForm.php
@@ -323,7 +323,7 @@ protected function validateVisibility(array $form, FormStateInterface $form_stat
// However, certain form elements may return it as 0/1. Cast here to
// ensure the data is in the expected type.
if (array_key_exists('negate', $values)) {
- $values['negate'] = (bool) $values['negate'];
+ $form_state->setValue(['visibility', $condition_id, 'negate'], (bool) $values['negate']);
}
// Allow the condition to validate the form.
diff --git a/core/modules/block/src/Tests/Update/BlockConditionMissingSchemaUpdateTest.php b/core/modules/block/src/Tests/Update/BlockConditionMissingSchemaUpdateTest.php
new file mode 100644
index 0000000..8eef2e3
--- /dev/null
+++ b/core/modules/block/src/Tests/Update/BlockConditionMissingSchemaUpdateTest.php
@@ -0,0 +1,53 @@
+<?php
+
+namespace Drupal\block\Tests\Update;
+
+use Drupal\system\Tests\Update\UpdatePathTestBase;
+
+/**
+ * Tests the upgrade path for block with conditions missing context.
+ *
+ * @see https://www.drupal.org/node/2811519
+ *
+ * @group Update
+ */
+class BlockConditionMissingSchemaUpdateTest extends UpdatePathTestBase {
+
+ /**
+ * This test does not have a failed update but the configuration has missing
+ * schema so can not do the full post update testing offered by
+ * UpdatePathTestBase.
+ *
+ * @var bool
+ *
+ * @see \Drupal\system\Tests\Update\UpdatePathTestBase::runUpdates()
+ */
+ protected $checkFailedUpdates = FALSE;
+
+ /**
+ * {@inheritdoc}
+ */
+ protected static $modules = ['block_test', 'language'];
+
+ /**
+ * {@inheritdoc}
+ */
+ protected function setDatabaseDumpFiles() {
+ $this->databaseDumpFiles = [
+ __DIR__ . '/../../../../system/tests/fixtures/update/drupal-8.bare.standard.php.gz',
+ __DIR__ . '/../../../tests/fixtures/update/drupal-8.block-test-enabled-missing-schema.php',
+ ];
+ }
+
+ /**
+ * Tests that block context mapping is updated properly.
+ */
+ public function testUpdateHookN() {
+ $this->runUpdates();
+ $this->drupalGet('<front>');
+ // If the block is fixed by block_post_update_fix_negate_in_conditions()
+ // then it will be visible.
+ $this->assertText('Test missing schema on conditions');
+ }
+
+}
diff --git a/core/modules/block/tests/fixtures/update/block.block.missing_schema.yml b/core/modules/block/tests/fixtures/update/block.block.missing_schema.yml
new file mode 100644
index 0000000..103692a
--- /dev/null
+++ b/core/modules/block/tests/fixtures/update/block.block.missing_schema.yml
@@ -0,0 +1,28 @@
+uuid: 1a6c0f14-78dc-4ede-bade-b8ce83881453
+langcode: en
+status: true
+dependencies:
+ module:
+ - block_test
+ - system
+ theme:
+ - bartik
+id: missing_schema
+theme: bartik
+region: sidebar_first
+weight: 0
+provider: null
+plugin: system_branding_block
+settings:
+ id: system_branding_block
+ label: 'Test missing schema on conditions'
+ provider: system
+ label_display: visible
+ use_site_logo: true
+ use_site_name: true
+ use_site_slogan: true
+visibility:
+ missing_schema:
+ id: missing_schema
+ negate: 0
+ context_mapping: { }
diff --git a/core/modules/block/tests/fixtures/update/drupal-8.block-test-enabled-missing-schema.php b/core/modules/block/tests/fixtures/update/drupal-8.block-test-enabled-missing-schema.php
new file mode 100644
index 0000000..c21628a
--- /dev/null
+++ b/core/modules/block/tests/fixtures/update/drupal-8.block-test-enabled-missing-schema.php
@@ -0,0 +1,49 @@
+<?php
+
+/**
+ * @file
+ * Partial database to mimic the installation of the block_test module.
+ */
+
+use Drupal\Core\Database\Database;
+use Symfony\Component\Yaml\Yaml;
+
+$connection = Database::getConnection();
+
+// Set the schema version.
+$connection->insert('key_value')
+ ->fields([
+ 'collection' => 'system.schema',
+ 'name' => 'block_test',
+ 'value' => 'i:8000;',
+ ])
+ ->execute();
+
+// Update core.extension.
+$extensions = $connection->select('config')
+ ->fields('config', ['data'])
+ ->condition('collection', '')
+ ->condition('name', 'core.extension')
+ ->execute()
+ ->fetchField();
+$extensions = unserialize($extensions);
+$extensions['module']['block_test'] = 8000;
+$connection->update('config')
+ ->fields([
+ 'data' => serialize($extensions),
+ ])
+ ->condition('collection', '')
+ ->condition('name', 'core.extension')
+ ->execute();
+
+// Install the block configuration.
+$config = file_get_contents(__DIR__ . '/block.block.missing_schema.yml');
+$config = Yaml::parse($config);
+$connection->insert('config')
+ ->fields(['data', 'name', 'collection'])
+ ->values([
+ 'name' => 'block.block.missing_schema',
+ 'data' => serialize($config),
+ 'collection' => '',
+ ])
+ ->execute();
diff --git a/core/modules/block/tests/modules/block_test/src/Plugin/Condition/MissingSchema.php b/core/modules/block/tests/modules/block_test/src/Plugin/Condition/MissingSchema.php
new file mode 100644
index 0000000..977374c
--- /dev/null
+++ b/core/modules/block/tests/modules/block_test/src/Plugin/Condition/MissingSchema.php
@@ -0,0 +1,31 @@
+<?php
+
+namespace Drupal\block_test\Plugin\Condition;
+
+use Drupal\Core\Condition\ConditionPluginBase;
+
+/**
+ * Provides a 'missing_schema' condition.
+ *
+ * @Condition(
+ * id = "missing_schema",
+ * label = @Translation("Missing schema"),
+ * )
+ */
+class MissingSchema extends ConditionPluginBase {
+
+ /**
+ * {@inheritdoc}
+ */
+ public function evaluate() {
+ return FALSE;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function summary() {
+ return 'Summary';
+ }
+
+}
diff --git a/core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php b/core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php
index 644a76b..27afd30 100644
--- a/core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php
+++ b/core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php
@@ -23,6 +23,10 @@ class ModerationStateFieldItemList extends EntityReferenceFieldItemList {
protected function getModerationState() {
$entity = $this->getEntity();
+ if (!\Drupal::service('content_moderation.moderation_information')->shouldModerateEntitiesOfBundle($entity->getEntityType(), $entity->bundle())) {
+ return NULL;
+ }
+
if ($entity->id() && $entity->getRevisionId()) {
$revisions = \Drupal::service('entity.query')->get('content_moderation_state')
->condition('content_entity_type_id', $entity->getEntityTypeId())
diff --git a/core/modules/content_moderation/src/Tests/ModerationStateNodeTest.php b/core/modules/content_moderation/src/Tests/ModerationStateNodeTest.php
index a819caf..e2069b4 100644
--- a/core/modules/content_moderation/src/Tests/ModerationStateNodeTest.php
+++ b/core/modules/content_moderation/src/Tests/ModerationStateNodeTest.php
@@ -47,6 +47,7 @@ public function testCreatingContent() {
}
$node = reset($nodes);
+ $this->assertEqual('draft', $node->moderation_state->target_id);
$path = 'node/' . $node->id() . '/edit';
// Set up published revision.
@@ -55,6 +56,7 @@ public function testCreatingContent() {
/* @var \Drupal\node\NodeInterface $node */
$node = \Drupal::entityTypeManager()->getStorage('node')->load($node->id());
$this->assertTrue($node->isPublished());
+ $this->assertEqual('published', $node->moderation_state->target_id);
// Verify that the state field is not shown.
$this->assertNoText('Published');
@@ -62,6 +64,31 @@ public function testCreatingContent() {
// Delete the node.
$this->drupalPostForm('node/' . $node->id() . '/delete', array(), t('Delete'));
$this->assertText(t('The Moderated content moderated content has been deleted.'));
+
+ $this->drupalGet('admin/structure/types/manage/moderated_content/moderation');
+ $this->assertFieldByName('enable_moderation_state');
+ $this->assertFieldChecked('edit-enable-moderation-state');
+ $this->drupalPostForm(NULL, ['enable_moderation_state' => FALSE], t('Save'));
+ $this->drupalGet('admin/structure/types/manage/moderated_content/moderation');
+ $this->assertFieldByName('enable_moderation_state');
+ $this->assertNoFieldChecked('edit-enable-moderation-state');
+ $this->drupalPostForm('node/add/moderated_content', [
+ 'title[0][value]' => 'non-moderated content',
+ ], t('Save and publish'));
+
+ $nodes = \Drupal::entityTypeManager()
+ ->getStorage('node')
+ ->loadByProperties([
+ 'title' => 'non-moderated content',
+ ]);
+
+ if (!$nodes) {
+ $this->fail('Non-moderated test node was not saved correctly.');
+ return;
+ }
+
+ $node = reset($nodes);
+ $this->assertEqual(NULL, $node->moderation_state->target_id);
}
/**
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment