- /**
- * Assert that edit mode has been properly enabled.
- */
- protected function assertEditModeEnabled() {
- $web_assert = $this->assertSession();
- // No contextual triggers should be hidden.
- $web_assert->elementNotExists('css', '.contextual .trigger.visually-hidden');
- // The toolbar edit button should read "Editing".
- $web_assert->elementContains('css', static::TOOLBAR_EDIT_LINK_SELECTOR, 'Editing');
- // The main canvas element should have the "js-settings-tray-edit-mode" class.
- $web_assert->elementExists('css', '.dialog-off-canvas-main-canvas.js-settings-tray-edit-mode');
- }
-
- /**
- * Assert that edit mode has been properly disabled.
- */
- protected function assertEditModeDisabled() {
- $web_assert = $this->assertSession();
- // Contextual triggers should be hidden.
- $web_assert->elementExists('css', '.contextual .trigger.visually-hidden');
- // No contextual triggers should be not hidden.
- $web_assert->elementNotExists('css', '.contextual .trigger:not(.visually-hidden)');
- // The toolbar edit button should read "Edit".
- $web_assert->elementContains('css', static::TOOLBAR_EDIT_LINK_SELECTOR, 'Edit');
- // The main canvas element should NOT have the "js-settings-tray-edit-mode"
- // class.
- $web_assert->elementNotExists('css', '.dialog-off-canvas-main-canvas.js-settings-tray-edit-mode');
- }
-
- /**
- * Press the toolbar Edit button provided by the contextual module.
- */
- protected function pressToolbarEditButton() {
- $this->assertSession()->waitForElement('css', '[data-contextual-id] .contextual-links a');
- $edit_button = $this->getSession()
- ->getPage()
- ->find('css', static::TOOLBAR_EDIT_LINK_SELECTOR);
- $edit_button->press();
- }
-
- /**
- * Creates a custom block.
- *
- * @param bool|string $title
- * (optional) Title of block. When no value is given uses a random name.
- * Defaults to FALSE.
- * @param string $bundle
- * (optional) Bundle name. Defaults to 'basic'.
- * @param bool $save
- * (optional) Whether to save the block. Defaults to TRUE.
- *
- * @return \Drupal\block_content\Entity\BlockContent
- * Created custom block.
- */
- protected function createBlockContent($title = FALSE, $bundle = 'basic', $save = TRUE) {
- $title = $title ?: $this->randomName();
- $block_content = BlockContent::create([
- 'info' => $title,
- 'type' => $bundle,
- 'langcode' => 'en',
- 'body' => [
- 'value' => 'The name "llama" was adopted by European settlers from native Peruvians.',
- 'format' => 'plain_text',
- ],
- ]);
- if ($block_content && $save === TRUE) {
- $block_content->save();
- }
- return $block_content;
- }
-
- /**
- * Creates a custom block type (bundle).
- *
- * @param string $label
- * The block type label.
- * @param bool $create_body
- * Whether or not to create the body field.
- *
- * @return \Drupal\block_content\Entity\BlockContentType
- * Created custom block type.
- */
- protected function createBlockContentType($label, $create_body = FALSE) {
- $bundle = BlockContentType::create([
- 'id' => $label,
- 'label' => $label,
- 'revision' => FALSE,
- ]);
- $bundle->save();
- if ($create_body) {
- block_content_add_body_field($bundle->id());
- }
- return $bundle;
- }
-
- /**
- * Tests that contextual links in custom blocks are changed.
- *
- * "Quick edit" is quickedit.module link.
- * "Quick edit settings" is settings_tray.module link.
- */
- public function testCustomBlockLinks() {
- $this->container->get('module_installer')->install(['quickedit']);
- $this->grantPermissions(Role::load(RoleInterface::AUTHENTICATED_ID), ['access in-place editing']);
- $this->drupalGet('user');
- $page = $this->getSession()->getPage();
- $links = $page->findAll('css', "#block-custom .contextual-links li a");
- $link_labels = [];
- /** @var \Behat\Mink\Element\NodeElement $link */
- foreach ($links as $link) {
- $link_labels[$link->getAttribute('href')] = $link->getText();
- }
- $href = array_search('Quick edit', $link_labels);
- $this->assertEquals('', $href);
- $href = array_search('Quick edit settings', $link_labels);
- $this->assertTrue(strstr($href, '/admin/structure/block/manage/custom/settings-tray?destination=user/2') !== FALSE);
- }
-
- /**
- * Gets the block CSS selector.
- *
- * @param \Drupal\block\Entity\Block $block
- * The block.
- *
- * @return string
- * The CSS selector.
- */
- public function getBlockSelector(Block $block) {
- return '#block-' . str_replace('_', '-', $block->id());
- }
-
- /**
- * Determines if the label input is visible.
- *
- * @return bool
- * TRUE if the label is visible, FALSE if it is not.
- */
- protected function isLabelInputVisible() {
- return $this->getSession()->getPage()->find('css', static::LABEL_INPUT_SELECTOR)->isVisible();
- }
-
- /**
- * Tests access to block forms with related configuration is correct.
- */
- public function testBlockConfigAccess() {
- $page = $this->getSession()->getPage();
- $web_assert = $this->assertSession();
-
- // Confirm that System Branding block does not expose Site Name field
- // without permission.
- $block = $this->placeBlock('system_branding_block');
- $this->drupalGet('user');
- $this->enableEditMode();
- $this->openBlockForm($this->getBlockSelector($block));
- // The site name field should not appear because the user doesn't have
- // permission.
- $web_assert->fieldNotExists('settings[site_information][site_name]');
- $page->pressButton('Save Site branding');
- $this->assertElementVisibleAfterWait('css', 'div:contains(The block configuration has been saved)');
- $web_assert->assertWaitOnAjaxRequest();
- // Confirm we did not save changes to the configuration.
- $this->assertEquals('Drupal', \Drupal::configFactory()->getEditable('system.site')->get('name'));
-
- $this->grantPermissions(Role::load(Role::AUTHENTICATED_ID), ['administer site configuration']);
- $this->drupalGet('user');
- $this->openBlockForm($this->getBlockSelector($block));
- // The site name field should appear because the user does have permission.
- $web_assert->fieldExists('settings[site_information][site_name]');
-
- // Confirm that the Menu block does not expose menu configuration without
- // permission.
- // Add a link or the menu will not render.
- $menu_link_content = MenuLinkContent::create([
- 'title' => 'This is on the menu',
- 'menu_name' => 'main',
- 'link' => ['uri' => 'route:<front>'],
- ]);
- $menu_link_content->save();
- $this->assertNotEmpty($menu_link_content->isEnabled());
- $menu_without_overrides = \Drupal::configFactory()->getEditable('system.menu.main')->get();
- $block = $this->placeBlock('system_menu_block:main');
- $this->drupalGet('user');
- $web_assert->pageTextContains('This is on the menu');
- $this->openBlockForm($this->getBlockSelector($block));
- // Edit menu form should not appear because the user doesn't have
- // permission.
- $web_assert->pageTextNotContains('Edit menu');
- $page->pressButton('Save Main navigation');
- $this->assertElementVisibleAfterWait('css', 'div:contains(The block configuration has been saved)');
- $web_assert->assertWaitOnAjaxRequest();
- // Confirm we did not save changes to the menu or the menu link.
- $this->assertEquals($menu_without_overrides, \Drupal::configFactory()->getEditable('system.menu.main')->get());
- $menu_link_content = MenuLinkContent::load($menu_link_content->id());
- $this->assertNotEmpty($menu_link_content->isEnabled());
- // Confirm menu is still on the page.
- $this->drupalGet('user');
- $web_assert->pageTextContains('This is on the menu');
-
- $this->grantPermissions(Role::load(Role::AUTHENTICATED_ID), ['administer menu']);
- $this->drupalGet('user');
- $web_assert->pageTextContains('This is on the menu');
- $this->openBlockForm($this->getBlockSelector($block));
- // Edit menu form should appear because the user does have permission.
- $web_assert->pageTextContains('Edit menu');
- }
-