function SettingsTrayBlockFormTest::doTestBlocks

Same name and namespace in other branches
  1. 8.9.x core/modules/settings_tray/tests/src/FunctionalJavascript/SettingsTrayBlockFormTest.php \Drupal\Tests\settings_tray\FunctionalJavascript\SettingsTrayBlockFormTest::doTestBlocks()
  2. 10 core/modules/settings_tray/tests/src/FunctionalJavascript/SettingsTrayBlockFormTest.php \Drupal\Tests\settings_tray\FunctionalJavascript\SettingsTrayBlockFormTest::doTestBlocks()
  3. 11.x core/modules/settings_tray/tests/src/FunctionalJavascript/SettingsTrayBlockFormTest.php \Drupal\Tests\settings_tray\FunctionalJavascript\SettingsTrayBlockFormTest::doTestBlocks()

Tests opening off-canvas dialog by click blocks and elements in the blocks.

File

core/modules/settings_tray/tests/src/FunctionalJavascript/SettingsTrayBlockFormTest.php, line 60

Class

SettingsTrayBlockFormTest
Testing opening and saving block forms in the off-canvas dialog.

Namespace

Drupal\Tests\settings_tray\FunctionalJavascript

Code

protected function doTestBlocks($theme, $block_plugin, $new_page_text, $element_selector, $label_selector, $button_text, $toolbar_item, $permissions) {
    if ($permissions) {
        $this->grantPermissions(Role::load(Role::AUTHENTICATED_ID), $permissions);
    }
    if ($new_page_text) {
        // Some asserts can be based on this value, so it should not be the same
        // for different blocks, because it can be saved in the site config.
        $new_page_text = $new_page_text . ' ' . $theme . ' ' . $block_plugin;
    }
    $web_assert = $this->assertSession();
    $page = $this->getSession()
        ->getPage();
    $this->enableTheme($theme);
    $block = $this->placeBlock($block_plugin);
    $block_selector = $this->getBlockSelector($block);
    $block_id = $block->id();
    $this->drupalGet('user');
    $link = $web_assert->waitForElement('css', "{$block_selector} .contextual-links li a");
    $this->assertEquals('Quick edit', $link->getHtml(), "'Quick edit' is the first contextual link for the block.");
    $destination = (string) $this->loggedInUser
        ->toUrl()
        ->toString();
    $this->assertStringContainsString("/admin/structure/block/manage/{$block_id}/settings-tray?destination={$destination}", $link->getAttribute('href'));
    if (isset($toolbar_item)) {
        // Check that you can open a toolbar tray and it will be closed after
        // entering edit mode.
        if ($element = $page->find('css', "#toolbar-administration a.is-active")) {
            // If a tray was open from page load close it.
            $element->click();
            $web_assert->assertNoElementAfterWait('css', "#toolbar-administration a.is-active");
        }
        $page->find('css', $toolbar_item)
            ->click();
        $this->assertElementVisibleAfterWait('css', "{$toolbar_item}.is-active");
    }
    $this->enableEditMode();
    if (isset($toolbar_item)) {
        $web_assert->assertNoElementAfterWait('css', "{$toolbar_item}.is-active");
    }
    $this->openBlockForm($block_selector);
    switch ($block_plugin) {
        case 'system_powered_by_block':
            // Confirm "Display Title" is not checked.
            $web_assert->checkboxNotChecked('settings[label_display]');
            // Confirm Title is not visible.
            $this->assertFalse($this->isLabelInputVisible(), 'Label is not visible');
            $page->checkField('settings[label_display]');
            $this->assertTrue($this->isLabelInputVisible(), 'Label is visible');
            // Fill out form, save the form.
            $page->fillField('settings[label]', $new_page_text);
            break;
        case 'system_branding_block':
            // Fill out form, save the form.
            $page->fillField('settings[site_information][site_name]', $new_page_text);
            break;
        case 'settings_tray_test_class':
            $web_assert->elementExists('css', '[data-drupal-selector="edit-settings-some-setting"]');
            break;
    }
    if (isset($new_page_text)) {
        $page->pressButton($button_text);
        // Make sure the changes are present.
        $new_page_text_locator = "{$block_selector} {$label_selector}:contains({$new_page_text})";
        $this->assertElementVisibleAfterWait('css', $new_page_text_locator);
        // The page is loaded with the new change but make sure page is
        // completely loaded.
        $this->assertPageLoadComplete();
    }
    $this->openBlockForm($block_selector);
    $this->disableEditMode();
    // Canvas should close when editing module is closed.
    $this->waitForOffCanvasToClose();
    $this->enableEditMode();
    // Open block form by clicking an element inside the block.
    // This confirms that default action for links and form elements is
    // suppressed.
    $this->openBlockForm("{$block_selector} {$element_selector}", $block_selector);
    $web_assert->elementTextContains('css', '.contextual-toolbar-tab button', 'Editing');
    $web_assert->elementAttributeContains('css', '.dialog-off-canvas-main-canvas', 'class', 'js-settings-tray-edit-mode');
    // Simulate press the Escape key.
    $this->getSession()
        ->executeScript('jQuery("body").trigger(jQuery.Event("keyup", { keyCode: 27 }));');
    $this->waitForOffCanvasToClose();
    $this->getSession()
        ->wait(100);
    $this->getSession()
        ->executeScript("jQuery('[data-quickedit-entity-id]').trigger('mouseleave')");
    $this->getSession()
        ->getPage()
        ->find('css', static::TOOLBAR_EDIT_LINK_SELECTOR)
        ->mouseOver();
    $this->assertEditModeDisabled();
    $this->assertNotEmpty($web_assert->waitForElement('css', '#drupal-live-announce:contains(Exited edit mode)'));
    $web_assert->assertNoElementAfterWait('css', '.contextual-toolbar-tab button:contains(Editing)');
    $web_assert->elementAttributeNotContains('css', '.dialog-off-canvas-main-canvas', 'class', 'js-settings-tray-edit-mode');
    // Clean up test data so each test does not impact the next.
    $block->delete();
    if ($permissions) {
        user_role_revoke_permissions(Role::AUTHENTICATED_ID, $permissions);
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.