Unverified Commit e47f5e75 authored by Lauri Timmanee's avatar Lauri Timmanee
Browse files

Issue #2843992 by smustgrave, jasonawant, moshnoi, alexpott: Block page...

Issue #2843992 by smustgrave, jasonawant, moshnoi, alexpott: Block page visibility paths are not validated, using path a without leading forward slash does not work
parent 753eef8a
Loading
Loading
Loading
Loading
+11 −0
Changes for core/modules/block/tests/src/Functional/BlockTest.php: 11 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -244,6 +244,17 @@ public function testBlock() {
    $xpath = $this->assertSession()->buildXPathQuery('//div[@id=:id]/*', [':id' => 'block-' . str_replace('_', '-', strtolower($block['id']))]);
    $this->assertSession()->elementNotExists('xpath', $xpath);

    $pages = [
      '',
      '<front>',
      '/valid-page',
      'user/login',
    ];
    // Test error when not including forward slash.
    $this->drupalGet('admin/structure/block/manage/' . $block['id']);
    $this->submitForm(['visibility[request_path][pages]' => implode("\n", $pages)], 'Save block');
    $this->assertSession()->pageTextContains('The path user/login requires a leading forward slash when used with the Pages setting.');

    // Test deleting the block from the edit form.
    $this->drupalGet('admin/structure/block/manage/' . $block['id']);
    $this->clickLink('Remove block');
+13 −0
Changes for core/modules/system/src/Plugin/Condition/RequestPath.php: 13 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -112,6 +112,19 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
    return parent::buildConfigurationForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
    $paths = array_map('trim', explode("\n", $form_state->getValue('pages')));
    foreach ($paths as $path) {
      if (empty($path) || $path === '<front>' || str_starts_with($path, '/')) {
        continue;
      }
      $form_state->setErrorByName('pages', $this->t("The path %path requires a leading forward slash when used with the Pages setting.", ['%path' => $path]));
    }
  }

  /**
   * {@inheritdoc}
   */