Move all files to 2017/
This commit is contained in:
parent
ac7370f67f
commit
2875863330
15717 changed files with 0 additions and 0 deletions
|
@ -0,0 +1,44 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\FunctionalJavascriptTests\Ajax;
|
||||
|
||||
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
|
||||
|
||||
/**
|
||||
* Tests Ajax callbacks on FAPI elements.
|
||||
*
|
||||
* @group Ajax
|
||||
*/
|
||||
class AjaxCallbacksTest extends WebDriverTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['ajax_forms_test'];
|
||||
|
||||
/**
|
||||
* Tests if Ajax callback works on date element.
|
||||
*/
|
||||
public function testDateAjaxCallback() {
|
||||
|
||||
// Test Ajax callback when date changes.
|
||||
$this->drupalGet('ajax_forms_test_ajax_element_form');
|
||||
$this->assertNotEmpty($this->getSession()->getPage()->find('xpath', '//div[@id="ajax_date_value"][text()="No date yet selected"]'));
|
||||
$this->getSession()->executeScript('jQuery("[data-drupal-selector=edit-date]").val("2016-01-01").trigger("change");');
|
||||
$this->assertNotEmpty($this->assertSession()->waitForElement('xpath', '//div[@id="ajax_date_value"]/div[text()="2016-01-01"]'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if Ajax callback works on datetime element.
|
||||
*/
|
||||
public function testDateTimeAjaxCallback() {
|
||||
|
||||
// Test Ajax callback when datetime changes.
|
||||
$this->drupalGet('ajax_forms_test_ajax_element_form');
|
||||
$this->assertNotEmpty($this->getSession()->getPage()->find('xpath', '//div[@id="ajax_datetime_value"][text()="No datetime selected."]'));
|
||||
$this->getSession()->executeScript('jQuery("[data-drupal-selector=edit-datetime-date]").val("2016-01-01");');
|
||||
$this->getSession()->executeScript('jQuery("[data-drupal-selector=edit-datetime-time]").val("12:00:00").trigger("change");');
|
||||
$this->assertNotEmpty($this->assertSession()->waitForElement('xpath', '//div[@id="ajax_datetime_value"]/div[text()="2016-01-01 12:00:00"]'));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,108 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\FunctionalJavascriptTests\Ajax;
|
||||
|
||||
use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
|
||||
use Drupal\Core\Form\FormBuilderInterface;
|
||||
use Drupal\Core\Url;
|
||||
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
|
||||
|
||||
/**
|
||||
* Tests the usage of form caching for AJAX forms.
|
||||
*
|
||||
* @group Ajax
|
||||
*/
|
||||
class AjaxFormCacheTest extends WebDriverTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['ajax_test', 'ajax_forms_test'];
|
||||
|
||||
/**
|
||||
* Tests the usage of form cache for AJAX forms.
|
||||
*/
|
||||
public function testFormCacheUsage() {
|
||||
/** @var \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface $key_value_expirable */
|
||||
$key_value_expirable = \Drupal::service('keyvalue.expirable')->get('form');
|
||||
$this->drupalLogin($this->rootUser);
|
||||
|
||||
// Ensure that the cache is empty.
|
||||
$this->assertEqual(0, count($key_value_expirable->getAll()));
|
||||
|
||||
// Visit an AJAX form that is not cached, 3 times.
|
||||
$uncached_form_url = Url::fromRoute('ajax_forms_test.commands_form');
|
||||
$this->drupalGet($uncached_form_url);
|
||||
$this->drupalGet($uncached_form_url);
|
||||
$this->drupalGet($uncached_form_url);
|
||||
|
||||
// The number of cache entries should not have changed.
|
||||
$this->assertEqual(0, count($key_value_expirable->getAll()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests AJAX forms in blocks.
|
||||
*/
|
||||
public function testBlockForms() {
|
||||
$this->container->get('module_installer')->install(['block', 'search']);
|
||||
$this->rebuildContainer();
|
||||
$this->container->get('router.builder')->rebuild();
|
||||
$this->drupalLogin($this->rootUser);
|
||||
|
||||
$this->drupalPlaceBlock('search_form_block', ['weight' => -5]);
|
||||
$this->drupalPlaceBlock('ajax_forms_test_block');
|
||||
|
||||
$this->drupalGet('');
|
||||
$session = $this->getSession();
|
||||
|
||||
// Select first option and trigger ajax update.
|
||||
$session->getPage()->selectFieldOption('edit-test1', 'option1');
|
||||
|
||||
// DOM update: The InsertCommand in the AJAX response changes the text
|
||||
// in the option element to 'Option1!!!'.
|
||||
$opt1_selector = $this->assertSession()->waitForElement('css', "select[data-drupal-selector='edit-test1'] option:contains('Option 1!!!')");
|
||||
$this->assertNotEmpty($opt1_selector);
|
||||
$this->assertTrue($opt1_selector->isSelected());
|
||||
|
||||
// Confirm option 3 exists.
|
||||
$page = $session->getPage();
|
||||
$opt3_selector = $page->find('xpath', '//select[@data-drupal-selector="edit-test1"]//option[@value="option3"]');
|
||||
$this->assertNotEmpty($opt3_selector);
|
||||
|
||||
// Confirm success message appears after a submit.
|
||||
$page->findButton('edit-submit')->click();
|
||||
$this->assertSession()->waitForButton('edit-submit');
|
||||
$updated_page = $session->getPage();
|
||||
$updated_page->hasContent('Submission successful.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests AJAX forms on pages with a query string.
|
||||
*/
|
||||
public function testQueryString() {
|
||||
$this->container->get('module_installer')->install(['block']);
|
||||
$this->drupalLogin($this->rootUser);
|
||||
|
||||
$this->drupalPlaceBlock('ajax_forms_test_block');
|
||||
|
||||
$url = Url::fromRoute('entity.user.canonical', ['user' => $this->rootUser->id()], ['query' => ['foo' => 'bar']]);
|
||||
$this->drupalGet($url);
|
||||
|
||||
$session = $this->getSession();
|
||||
// Select first option and trigger ajax update.
|
||||
$session->getPage()->selectFieldOption('edit-test1', 'option1');
|
||||
|
||||
// DOM update: The InsertCommand in the AJAX response changes the text
|
||||
// in the option element to 'Option1!!!'.
|
||||
$opt1_selector = $this->assertSession()->waitForElement('css', "option:contains('Option 1!!!')");
|
||||
$this->assertNotEmpty($opt1_selector);
|
||||
|
||||
$url->setOption('query', [
|
||||
'foo' => 'bar',
|
||||
FormBuilderInterface::AJAX_FORM_REQUEST => 1,
|
||||
MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax',
|
||||
]);
|
||||
$this->assertUrl($url);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\FunctionalJavascriptTests\Ajax;
|
||||
|
||||
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
|
||||
|
||||
/**
|
||||
* Tests the Ajax image buttons work with key press events.
|
||||
*
|
||||
* @group Ajax
|
||||
*/
|
||||
class AjaxFormImageButtonTest extends WebDriverTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['ajax_forms_test'];
|
||||
|
||||
/**
|
||||
* Tests image buttons can be operated with the keyboard ENTER key.
|
||||
*/
|
||||
public function testAjaxImageButton() {
|
||||
// Get a Field UI manage-display page.
|
||||
$this->drupalGet('ajax_forms_image_button_form');
|
||||
$assertSession = $this->assertSession();
|
||||
$session = $this->getSession();
|
||||
|
||||
$enter_key_event = <<<JS
|
||||
jQuery('#edit-image-button')
|
||||
.trigger(jQuery.Event('keypress', {
|
||||
which: 13
|
||||
}));
|
||||
JS;
|
||||
// PhantomJS driver has buggy behavior with key events, we send a JavaScript
|
||||
// key event instead.
|
||||
// @todo: use WebDriver event when we remove PhantomJS driver.
|
||||
$session->executeScript($enter_key_event);
|
||||
|
||||
$this->assertNotEmpty($assertSession->waitForElementVisible('css', '#ajax-1-more-div'), 'Page updated after image button pressed');
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,127 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\FunctionalJavascriptTests\Ajax;
|
||||
|
||||
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
|
||||
|
||||
/**
|
||||
* Performs tests on AJAX forms in cached pages.
|
||||
*
|
||||
* @group Ajax
|
||||
*/
|
||||
class AjaxFormPageCacheTest extends WebDriverTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['ajax_test', 'ajax_forms_test'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$config = $this->config('system.performance');
|
||||
$config->set('cache.page.max_age', 300);
|
||||
$config->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the build id of the current form.
|
||||
*/
|
||||
protected function getFormBuildId() {
|
||||
$build_id_fields = $this->xpath('//input[@name="form_build_id"]');
|
||||
$this->assertEquals(count($build_id_fields), 1, 'One form build id field on the page');
|
||||
return $build_id_fields[0]->getValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a simple form, then submit the form via AJAX to change to it.
|
||||
*/
|
||||
public function testSimpleAJAXFormValue() {
|
||||
$this->drupalGet('ajax_forms_test_get_form');
|
||||
$build_id_initial = $this->getFormBuildId();
|
||||
|
||||
// Changing the value of a select input element, triggers a AJAX
|
||||
// request/response. The callback on the form responds with three AJAX
|
||||
// commands:
|
||||
// - UpdateBuildIdCommand
|
||||
// - HtmlCommand
|
||||
// - DataCommand
|
||||
$session = $this->getSession();
|
||||
$session->getPage()->selectFieldOption('select', 'green');
|
||||
|
||||
// Wait for the DOM to update. The HtmlCommand will update
|
||||
// #ajax_selected_color to reflect the color change.
|
||||
$green_span = $this->assertSession()->waitForElement('css', "#ajax_selected_color:contains('green')");
|
||||
$this->assertNotNull($green_span, 'DOM update: The selected color SPAN is green.');
|
||||
|
||||
// Confirm the operation of the UpdateBuildIdCommand.
|
||||
$build_id_first_ajax = $this->getFormBuildId();
|
||||
$this->assertNotEquals($build_id_initial, $build_id_first_ajax, 'Build id is changed in the form_build_id element on first AJAX submission');
|
||||
|
||||
// Changing the value of a select input element, triggers a AJAX
|
||||
// request/response.
|
||||
$session->getPage()->selectFieldOption('select', 'red');
|
||||
|
||||
// Wait for the DOM to update.
|
||||
$red_span = $this->assertSession()->waitForElement('css', "#ajax_selected_color:contains('red')");
|
||||
$this->assertNotNull($red_span, 'DOM update: The selected color SPAN is red.');
|
||||
|
||||
// Confirm the operation of the UpdateBuildIdCommand.
|
||||
$build_id_second_ajax = $this->getFormBuildId();
|
||||
$this->assertNotEquals($build_id_first_ajax, $build_id_second_ajax, 'Build id changes on subsequent AJAX submissions');
|
||||
|
||||
// Emulate a push of the reload button and then repeat the test sequence
|
||||
// this time with a page loaded from the cache.
|
||||
$session->reload();
|
||||
$build_id_from_cache_initial = $this->getFormBuildId();
|
||||
$this->assertEquals($build_id_initial, $build_id_from_cache_initial, 'Build id is the same as on the first request');
|
||||
|
||||
// Changing the value of a select input element, triggers a AJAX
|
||||
// request/response.
|
||||
$session->getPage()->selectFieldOption('select', 'green');
|
||||
|
||||
// Wait for the DOM to update.
|
||||
$green_span2 = $this->assertSession()->waitForElement('css', "#ajax_selected_color:contains('green')");
|
||||
$this->assertNotNull($green_span2, 'DOM update: After reload - the selected color SPAN is green.');
|
||||
|
||||
$build_id_from_cache_first_ajax = $this->getFormBuildId();
|
||||
$this->assertNotEquals($build_id_from_cache_initial, $build_id_from_cache_first_ajax, 'Build id is changed in the simpletest-DOM on first AJAX submission');
|
||||
$this->assertNotEquals($build_id_first_ajax, $build_id_from_cache_first_ajax, 'Build id from first user is not reused');
|
||||
|
||||
// Changing the value of a select input element, triggers a AJAX
|
||||
// request/response.
|
||||
$session->getPage()->selectFieldOption('select', 'red');
|
||||
|
||||
// Wait for the DOM to update.
|
||||
$red_span2 = $this->assertSession()->waitForElement('css', "#ajax_selected_color:contains('red')");
|
||||
$this->assertNotNull($red_span2, 'DOM update: After reload - the selected color SPAN is red.');
|
||||
|
||||
$build_id_from_cache_second_ajax = $this->getFormBuildId();
|
||||
$this->assertNotEquals($build_id_from_cache_first_ajax, $build_id_from_cache_second_ajax, 'Build id changes on subsequent AJAX submissions');
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that updating the text field trigger an AJAX request/response.
|
||||
*
|
||||
* @see \Drupal\system\Tests\Ajax\ElementValidationTest::testAjaxElementValidation()
|
||||
*/
|
||||
public function testAjaxElementValidation() {
|
||||
$this->drupalGet('ajax_validation_test');
|
||||
// Changing the value of the textfield will trigger an AJAX
|
||||
// request/response.
|
||||
$field = $this->getSession()->getPage()->findField('drivertext');
|
||||
$field->setValue('some dumb text');
|
||||
$field->blur();
|
||||
|
||||
// When the AJAX command updates the DOM a <ul> unsorted list
|
||||
// "message__list" structure will appear on the page echoing back the
|
||||
// "some dumb text" message.
|
||||
$placeholder = $this->assertSession()->waitForElement('css', "ul.messages__list li.messages__item em:contains('some dumb text')");
|
||||
$this->assertNotNull($placeholder, 'Message structure containing input data located.');
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\FunctionalJavascriptTests\Ajax;
|
||||
|
||||
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
|
||||
|
||||
/**
|
||||
* Tests that form elements in groups work correctly with AJAX.
|
||||
*
|
||||
* @group Ajax
|
||||
*/
|
||||
class AjaxInGroupTest extends WebDriverTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $modules = ['ajax_forms_test'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->drupalLogin($this->drupalCreateUser(['access content']));
|
||||
}
|
||||
|
||||
/**
|
||||
* Submits forms with select and checkbox elements via Ajax.
|
||||
*/
|
||||
public function testSimpleAjaxFormValue() {
|
||||
$this->drupalGet('/ajax_forms_test_get_form');
|
||||
|
||||
$assert_session = $this->assertSession();
|
||||
$assert_session->responseContains('Test group');
|
||||
$assert_session->responseContains('AJAX checkbox in a group');
|
||||
|
||||
$session = $this->getSession();
|
||||
$checkbox_original = $session->getPage()->findField('checkbox_in_group');
|
||||
$this->assertNotNull($checkbox_original, 'The checkbox_in_group is on the page.');
|
||||
$original_id = $checkbox_original->getAttribute('id');
|
||||
|
||||
// Triggers a AJAX request/response.
|
||||
$checkbox_original->check();
|
||||
|
||||
// The response contains a new nested "test group" form element, similar
|
||||
// to the one already in the DOM except for a change in the form build id.
|
||||
$checkbox_new = $assert_session->waitForElement('xpath', "//input[@name='checkbox_in_group' and not(@id='$original_id')]");
|
||||
$this->assertNotNull($checkbox_new, 'DOM update: clicking the checkbox refreshed the checkbox_in_group structure');
|
||||
|
||||
$assert_session->responseContains('Test group');
|
||||
$assert_session->responseContains('AJAX checkbox in a group');
|
||||
$assert_session->responseContains('AJAX checkbox in a nested group');
|
||||
$assert_session->responseContains('Another AJAX checkbox in a nested group');
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,200 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\FunctionalJavascriptTests\Ajax;
|
||||
|
||||
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
|
||||
|
||||
/**
|
||||
* Tests AJAX responses.
|
||||
*
|
||||
* @group Ajax
|
||||
*/
|
||||
class AjaxTest extends WebDriverTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['ajax_test'];
|
||||
|
||||
public function testAjaxWithAdminRoute() {
|
||||
\Drupal::service('theme_installer')->install(['stable', 'seven']);
|
||||
$theme_config = \Drupal::configFactory()->getEditable('system.theme');
|
||||
$theme_config->set('admin', 'seven');
|
||||
$theme_config->set('default', 'stable');
|
||||
$theme_config->save();
|
||||
|
||||
$account = $this->drupalCreateUser(['view the administration theme']);
|
||||
$this->drupalLogin($account);
|
||||
|
||||
// First visit the site directly via the URL. This should render it in the
|
||||
// admin theme.
|
||||
$this->drupalGet('admin/ajax-test/theme');
|
||||
$assert = $this->assertSession();
|
||||
$assert->pageTextContains('Current theme: seven');
|
||||
|
||||
// Now click the modal, which should also use the admin theme.
|
||||
$this->drupalGet('ajax-test/dialog');
|
||||
$assert->pageTextNotContains('Current theme: stable');
|
||||
$this->clickLink('Link 8 (ajax)');
|
||||
$assert->assertWaitOnAjaxRequest();
|
||||
|
||||
$assert->pageTextContains('Current theme: stable');
|
||||
$assert->pageTextNotContains('Current theme: seven');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that AJAX loaded libraries are not retained between requests.
|
||||
*
|
||||
* @see https://www.drupal.org/node/2647916
|
||||
*/
|
||||
public function testDrupalSettingsCachingRegression() {
|
||||
$this->drupalGet('ajax-test/dialog');
|
||||
$assert = $this->assertSession();
|
||||
$session = $this->getSession();
|
||||
|
||||
// Insert a fake library into the already loaded library settings.
|
||||
$fake_library = 'fakeLibrary/fakeLibrary';
|
||||
$session->evaluateScript("drupalSettings.ajaxPageState.libraries = drupalSettings.ajaxPageState.libraries + ',$fake_library';");
|
||||
|
||||
$libraries = $session->evaluateScript('drupalSettings.ajaxPageState.libraries');
|
||||
// Test that the fake library is set.
|
||||
$this->assertContains($fake_library, $libraries);
|
||||
|
||||
// Click on the AJAX link.
|
||||
$this->clickLink('Link 8 (ajax)');
|
||||
$assert->assertWaitOnAjaxRequest();
|
||||
|
||||
// Test that the fake library is still set after the AJAX call.
|
||||
$libraries = $session->evaluateScript('drupalSettings.ajaxPageState.libraries');
|
||||
$this->assertContains($fake_library, $libraries);
|
||||
|
||||
// Reload the page, this should reset the loaded libraries and remove the
|
||||
// fake library.
|
||||
$this->drupalGet('ajax-test/dialog');
|
||||
$libraries = $session->evaluateScript('drupalSettings.ajaxPageState.libraries');
|
||||
$this->assertNotContains($fake_library, $libraries);
|
||||
|
||||
// Click on the AJAX link again, and the libraries should still not contain
|
||||
// the fake library.
|
||||
$this->clickLink('Link 8 (ajax)');
|
||||
$assert->assertWaitOnAjaxRequest();
|
||||
$libraries = $session->evaluateScript('drupalSettings.ajaxPageState.libraries');
|
||||
$this->assertNotContains($fake_library, $libraries);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that various AJAX responses with DOM elements are correctly inserted.
|
||||
*
|
||||
* After inserting DOM elements, Drupal JavaScript behaviors should be
|
||||
* reattached and all top-level elements of type Node.ELEMENT_NODE need to be
|
||||
* part of the context.
|
||||
*/
|
||||
public function testInsertAjaxResponse() {
|
||||
$render_single_root = [
|
||||
'pre-wrapped-div' => '<div class="pre-wrapped">pre-wrapped<script> var test;</script></div>',
|
||||
'pre-wrapped-span' => '<span class="pre-wrapped">pre-wrapped<script> var test;</script></span>',
|
||||
'pre-wrapped-whitespace' => ' <div class="pre-wrapped-whitespace">pre-wrapped-whitespace</div>' . "\n",
|
||||
'not-wrapped' => 'not-wrapped',
|
||||
'comment-string-not-wrapped' => '<!-- COMMENT -->comment-string-not-wrapped',
|
||||
'comment-not-wrapped' => '<!-- COMMENT --><div class="comment-not-wrapped">comment-not-wrapped</div>',
|
||||
'svg' => '<svg xmlns="http://www.w3.org/2000/svg" width="10" height="10"><rect x="0" y="0" height="10" width="10" fill="green"/></svg>',
|
||||
'empty' => '',
|
||||
];
|
||||
$render_multiple_root_unwrapper = [
|
||||
'mixed' => ' foo <!-- COMMENT --> foo bar<div class="a class"><p>some string</p></div> additional not wrapped strings, <!-- ANOTHER COMMENT --> <p>final string</p>',
|
||||
'top-level-only' => '<div>element #1</div><div>element #2</div>',
|
||||
'top-level-only-pre-whitespace' => ' <div>element #1</div><div>element #2</div> ',
|
||||
'top-level-only-middle-whitespace-span' => '<span>element #1</span> <span>element #2</span>',
|
||||
'top-level-only-middle-whitespace-div' => '<div>element #1</div> <div>element #2</div>',
|
||||
];
|
||||
|
||||
// This is temporary behavior for BC reason.
|
||||
$render_multiple_root_wrapper = [];
|
||||
foreach ($render_multiple_root_unwrapper as $key => $render) {
|
||||
$render_multiple_root_wrapper["$key--effect"] = '<div>' . $render . '</div>';
|
||||
}
|
||||
|
||||
$expected_renders = array_merge(
|
||||
$render_single_root,
|
||||
$render_multiple_root_wrapper,
|
||||
$render_multiple_root_unwrapper
|
||||
);
|
||||
|
||||
// Checking default process of wrapping Ajax content.
|
||||
foreach ($expected_renders as $render_type => $expected) {
|
||||
$this->assertInsert($render_type, $expected);
|
||||
}
|
||||
|
||||
// Checking custom ajaxWrapperMultipleRootElements wrapping.
|
||||
$custom_wrapper_multiple_root = <<<JS
|
||||
(function($, Drupal){
|
||||
Drupal.theme.ajaxWrapperMultipleRootElements = function (elements) {
|
||||
return $('<div class="my-favorite-div"></div>').append(elements);
|
||||
};
|
||||
}(jQuery, Drupal));
|
||||
JS;
|
||||
$expected = '<div class="my-favorite-div"><span>element #1</span> <span>element #2</span></div>';
|
||||
$this->assertInsert('top-level-only-middle-whitespace-span--effect', $expected, $custom_wrapper_multiple_root);
|
||||
|
||||
// Checking custom ajaxWrapperNewContent wrapping.
|
||||
$custom_wrapper_new_content = <<<JS
|
||||
(function($, Drupal){
|
||||
Drupal.theme.ajaxWrapperNewContent = function (elements) {
|
||||
return $('<div class="div-wrapper-forever"></div>').append(elements);
|
||||
};
|
||||
}(jQuery, Drupal));
|
||||
JS;
|
||||
$expected = '<div class="div-wrapper-forever"></div>';
|
||||
$this->assertInsert('empty', $expected, $custom_wrapper_new_content);
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert insert.
|
||||
*
|
||||
* @param string $render_type
|
||||
* Render type.
|
||||
* @param string $expected
|
||||
* Expected result.
|
||||
* @param string $script
|
||||
* Script for additional theming.
|
||||
*/
|
||||
public function assertInsert($render_type, $expected, $script = '') {
|
||||
// Check insert to block element.
|
||||
$this->drupalGet('ajax-test/insert-block-wrapper');
|
||||
$this->getSession()->executeScript($script);
|
||||
$this->clickLink("Link html $render_type");
|
||||
$this->assertWaitPageContains('<div class="ajax-target-wrapper"><div id="ajax-target">' . $expected . '</div></div>');
|
||||
|
||||
$this->drupalGet('ajax-test/insert-block-wrapper');
|
||||
$this->getSession()->executeScript($script);
|
||||
$this->clickLink("Link replaceWith $render_type");
|
||||
$this->assertWaitPageContains('<div class="ajax-target-wrapper">' . $expected . '</div>');
|
||||
|
||||
// Check insert to inline element.
|
||||
$this->drupalGet('ajax-test/insert-inline-wrapper');
|
||||
$this->getSession()->executeScript($script);
|
||||
$this->clickLink("Link html $render_type");
|
||||
$this->assertWaitPageContains('<div class="ajax-target-wrapper"><span id="ajax-target-inline">' . $expected . '</span></div>');
|
||||
|
||||
$this->drupalGet('ajax-test/insert-inline-wrapper');
|
||||
$this->getSession()->executeScript($script);
|
||||
$this->clickLink("Link replaceWith $render_type");
|
||||
$this->assertWaitPageContains('<div class="ajax-target-wrapper">' . $expected . '</div>');
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that page contains an expected value after waiting.
|
||||
*
|
||||
* @param string $expected
|
||||
* A needle text.
|
||||
*/
|
||||
protected function assertWaitPageContains($expected) {
|
||||
$page = $this->getSession()->getPage();
|
||||
$this->assertTrue($page->waitFor(10, function () use ($page, $expected) {
|
||||
// Clear content from empty styles and "processed" classes after effect.
|
||||
$content = str_replace([' class="processed"', ' processed', ' style=""'], '', $page->getContent());
|
||||
return stripos($content, $expected) !== FALSE;
|
||||
}), "Page contains expected value: $expected");
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\FunctionalJavascriptTests\Ajax;
|
||||
|
||||
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
|
||||
|
||||
/**
|
||||
* Tests the compatibility of the ajax.es6.js file.
|
||||
*
|
||||
* @group Ajax
|
||||
*/
|
||||
class BackwardCompatibilityTest extends WebDriverTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = [
|
||||
'js_ajax_test',
|
||||
];
|
||||
|
||||
/**
|
||||
* Ensures Drupal.Ajax.element_settings BC layer.
|
||||
*/
|
||||
public function testAjaxBackwardCompatibility() {
|
||||
$this->drupalGet('/js_ajax_test');
|
||||
$this->click('#edit-test-button');
|
||||
|
||||
$this->assertSession()
|
||||
->waitForElement('css', '#js_ajax_test_form_element');
|
||||
$elements = $this->cssSelect('#js_ajax_test_form_element');
|
||||
$this->assertCount(1, $elements);
|
||||
$json = $elements[0]->getText();
|
||||
$data = json_decode($json, TRUE);
|
||||
$this->assertEquals([
|
||||
'element_settings' => 'catbro',
|
||||
'elementSettings' => 'catbro',
|
||||
], $data);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,130 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\FunctionalJavascriptTests\Ajax;
|
||||
|
||||
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
|
||||
|
||||
/**
|
||||
* Performs tests on AJAX framework commands.
|
||||
*
|
||||
* @group Ajax
|
||||
*/
|
||||
class CommandsTest extends WebDriverTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['node', 'ajax_test', 'ajax_forms_test'];
|
||||
|
||||
/**
|
||||
* Tests the various Ajax Commands.
|
||||
*/
|
||||
public function testAjaxCommands() {
|
||||
$session = $this->getSession();
|
||||
$page = $this->getSession()->getPage();
|
||||
|
||||
$form_path = 'ajax_forms_test_ajax_commands_form';
|
||||
$web_user = $this->drupalCreateUser(['access content']);
|
||||
$this->drupalLogin($web_user);
|
||||
$this->drupalGet($form_path);
|
||||
|
||||
// Tests the 'add_css' command.
|
||||
$page->pressButton("AJAX 'add_css' command");
|
||||
$this->assertWaitPageContains('my/file.css');
|
||||
|
||||
// Tests the 'after' command.
|
||||
$page->pressButton("AJAX 'After': Click to put something after the div");
|
||||
$this->assertWaitPageContains('<div id="after_div">Something can be inserted after this</div>This will be placed after');
|
||||
|
||||
// Tests the 'alert' command.
|
||||
$test_alert_command = <<<JS
|
||||
window.alert = function() {
|
||||
document.body.innerHTML += '<div class="alert-command">Alert</div>';
|
||||
};
|
||||
JS;
|
||||
$session->executeScript($test_alert_command);
|
||||
$page->pressButton("AJAX 'Alert': Click to alert");
|
||||
$this->assertWaitPageContains('<div class="alert-command">Alert</div>');
|
||||
|
||||
// Tests the 'append' command.
|
||||
$page->pressButton("AJAX 'Append': Click to append something");
|
||||
$this->assertWaitPageContains('<div id="append_div">Append inside this divAppended text</div>');
|
||||
|
||||
// Tests the 'before' command.
|
||||
$page->pressButton("AJAX 'before': Click to put something before the div");
|
||||
$this->assertWaitPageContains('Before text<div id="before_div">Insert something before this.</div>');
|
||||
|
||||
// Tests the 'changed' command.
|
||||
$page->pressButton("AJAX changed: Click to mark div changed.");
|
||||
$this->assertWaitPageContains('<div id="changed_div" class="ajax-changed">');
|
||||
|
||||
// Tests the 'changed' command using the second argument.
|
||||
// Refresh page for testing 'changed' command to same element again.
|
||||
$this->drupalGet($form_path);
|
||||
$page->pressButton("AJAX changed: Click to mark div changed with asterisk.");
|
||||
$this->assertWaitPageContains('<div id="changed_div" class="ajax-changed"> <div id="changed_div_mark_this">This div can be marked as changed or not. <abbr class="ajax-changed" title="Changed">*</abbr> </div></div>');
|
||||
|
||||
// Tests the 'css' command.
|
||||
$page->pressButton("Set the '#box' div to be blue.");
|
||||
$this->assertWaitPageContains('<div id="css_div" style="background-color: blue;">');
|
||||
|
||||
// Tests the 'data' command.
|
||||
$page->pressButton("AJAX data command: Issue command.");
|
||||
$this->assertTrue($page->waitFor(10, function () use ($session) {
|
||||
return 'testvalue' === $session->evaluateScript('window.jQuery("#data_div").data("testkey")');
|
||||
}));
|
||||
|
||||
// Tests the 'html' command.
|
||||
$page->pressButton("AJAX html: Replace the HTML in a selector.");
|
||||
$this->assertWaitPageContains('<div id="html_div">replacement text</div>');
|
||||
|
||||
// Tests the 'insert' command.
|
||||
$page->pressButton("AJAX insert: Let client insert based on #ajax['method'].");
|
||||
$this->assertWaitPageContains('<div id="insert_div">insert replacement textOriginal contents</div>');
|
||||
|
||||
// Tests the 'invoke' command.
|
||||
$page->pressButton("AJAX invoke command: Invoke addClass() method.");
|
||||
$this->assertWaitPageContains('<div id="invoke_div" class="error">Original contents</div>');
|
||||
|
||||
// Tests the 'prepend' command.
|
||||
$page->pressButton("AJAX 'prepend': Click to prepend something");
|
||||
$this->assertWaitPageContains('<div id="prepend_div">prepended textSomething will be prepended to this div. </div>');
|
||||
|
||||
// Tests the 'remove' command.
|
||||
$page->pressButton("AJAX 'remove': Click to remove text");
|
||||
$this->assertWaitPageContains('<div id="remove_div"></div>');
|
||||
|
||||
// Tests the 'restripe' command.
|
||||
$page->pressButton("AJAX 'restripe' command");
|
||||
$this->assertWaitPageContains('<tr id="table-first" class="odd"><td>first row</td></tr>');
|
||||
$this->assertWaitPageContains('<tr class="even"><td>second row</td></tr>');
|
||||
|
||||
// Tests the 'settings' command.
|
||||
$test_settings_command = <<<JS
|
||||
Drupal.behaviors.testSettingsCommand = {
|
||||
attach: function (context, settings) {
|
||||
window.jQuery('body').append('<div class="test-settings-command">' + settings.ajax_forms_test.foo + '</div>');
|
||||
}
|
||||
};
|
||||
JS;
|
||||
$session->executeScript($test_settings_command);
|
||||
// @todo: Replace after https://www.drupal.org/project/drupal/issues/2616184
|
||||
$session->executeScript('window.jQuery("#edit-settings-command-example").mousedown();');
|
||||
$this->assertWaitPageContains('<div class="test-settings-command">42</div>');
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that page contains a text after waiting.
|
||||
*
|
||||
* @param string $text
|
||||
* A needle text.
|
||||
*/
|
||||
protected function assertWaitPageContains($text) {
|
||||
$page = $this->getSession()->getPage();
|
||||
$page->waitFor(10, function () use ($page, $text) {
|
||||
return stripos($page->getContent(), $text) !== FALSE;
|
||||
});
|
||||
$this->assertContains($text, $page->getContent());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,167 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\FunctionalJavascriptTests\Ajax;
|
||||
|
||||
use Drupal\ajax_test\Controller\AjaxTestController;
|
||||
use Drupal\Component\Render\FormattableMarkup;
|
||||
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
|
||||
|
||||
/**
|
||||
* Performs tests on opening and manipulating dialogs via AJAX commands.
|
||||
*
|
||||
* @group Ajax
|
||||
*/
|
||||
class DialogTest extends WebDriverTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $modules = ['ajax_test', 'ajax_forms_test', 'contact'];
|
||||
|
||||
/**
|
||||
* Test sending non-JS and AJAX requests to open and manipulate modals.
|
||||
*/
|
||||
public function testDialog() {
|
||||
$this->drupalLogin($this->drupalCreateUser(['administer contact forms']));
|
||||
// Ensure the elements render without notices or exceptions.
|
||||
$this->drupalGet('ajax-test/dialog');
|
||||
|
||||
// Set up variables for this test.
|
||||
$dialog_renderable = AjaxTestController::dialogContents();
|
||||
$dialog_contents = \Drupal::service('renderer')->renderRoot($dialog_renderable);
|
||||
|
||||
// Check that requesting a modal dialog without JS goes to a page.
|
||||
$this->drupalGet('ajax-test/dialog-contents');
|
||||
$this->assertSession()->responseContains($dialog_contents);
|
||||
|
||||
// Visit the page containing the many test dialog links.
|
||||
$this->drupalGet('ajax-test/dialog');
|
||||
|
||||
// Tests a basic modal dialog by verifying the contents of the dialog are as
|
||||
// expected.
|
||||
$this->getSession()->getPage()->clickLink('Link 1 (modal)');
|
||||
|
||||
// Clicking the link triggers a AJAX request/response.
|
||||
// Opens a Dialog panel.
|
||||
$link1_dialog_div = $this->assertSession()->waitForElementVisible('css', 'div.ui-dialog');
|
||||
$this->assertNotNull($link1_dialog_div, 'Link was used to open a dialog ( modal )');
|
||||
|
||||
$link1_modal = $link1_dialog_div->find('css', '#drupal-modal');
|
||||
$this->assertNotNull($link1_modal, 'Link was used to open a dialog ( non-modal )');
|
||||
$this->assertSession()->responseContains($dialog_contents);
|
||||
|
||||
$dialog_title = $link1_dialog_div->find('css', "span.ui-dialog-title:contains('AJAX Dialog & contents')");
|
||||
$this->assertNotNull($dialog_title);
|
||||
$dialog_title_amp = $link1_dialog_div->find('css', "span.ui-dialog-title:contains('AJAX Dialog & contents')");
|
||||
$this->assertNull($dialog_title_amp);
|
||||
|
||||
// Close open dialog, return to the dialog links page.
|
||||
$close_button = $link1_dialog_div->findButton('Close');
|
||||
$this->assertNotNull($close_button);
|
||||
$close_button->press();
|
||||
|
||||
// Tests a modal with a dialog-option.
|
||||
// Link 2 is similar to Link 1, except it submits additional width
|
||||
// information which must be echoed in the resulting DOM update.
|
||||
$this->getSession()->getPage()->clickLink('Link 2 (modal)');
|
||||
$dialog = $this->assertSession()->waitForElementVisible('css', 'div.ui-dialog');
|
||||
$this->assertNotNull($dialog, 'Link was used to open a dialog ( non-modal, with options )');
|
||||
$style = $dialog->getAttribute('style');
|
||||
$this->assertContains('width: 400px;', $style, new FormattableMarkup('Modal respected the dialog-options width parameter. Style = style', ['%style' => $style]));
|
||||
|
||||
// Reset: Return to the dialog links page.
|
||||
$this->drupalGet('ajax-test/dialog');
|
||||
|
||||
// Test a non-modal dialog ( with target ).
|
||||
$this->clickLink('Link 3 (non-modal)');
|
||||
$non_modal_dialog = $this->assertSession()->waitForElementVisible('css', 'div.ui-dialog');
|
||||
$this->assertNotNull($non_modal_dialog, 'Link opens a non-modal dialog.');
|
||||
|
||||
// Tests the dialog contains a target element specified in the AJAX request.
|
||||
$non_modal_dialog->find('css', 'div#ajax-test-dialog-wrapper-1');
|
||||
$this->assertSession()->responseContains($dialog_contents);
|
||||
|
||||
// Reset: Return to the dialog links page.
|
||||
$this->drupalGet('ajax-test/dialog');
|
||||
|
||||
// Tests a non-modal dialog ( without target ).
|
||||
$this->clickLink('Link 7 (non-modal, no target)');
|
||||
$no_target_dialog = $this->assertSession()->waitForElementVisible('css', 'div.ui-dialog');
|
||||
$this->assertNotNull($no_target_dialog, 'Link opens a non-modal dialog.');
|
||||
|
||||
$contents_no_target = $no_target_dialog->find('css', 'div.ui-dialog-content');
|
||||
$this->assertNotNull($contents_no_target, 'non-modal dialog opens ( no target ). ');
|
||||
$id = $contents_no_target->getAttribute('id');
|
||||
$partial_match = strpos($id, 'drupal-dialog-ajax-testdialog-contents') === 0;
|
||||
$this->assertTrue($partial_match, 'The non-modal ID has the expected prefix.');
|
||||
|
||||
$no_target_button = $no_target_dialog->findButton('Close');
|
||||
$this->assertNotNull($no_target_button, 'Link dialog has a close button');
|
||||
$no_target_button->press();
|
||||
|
||||
$this->getSession()->getPage()->findButton('Button 1 (modal)')->press();
|
||||
$button1_dialog = $this->assertSession()->waitForElementVisible('css', 'div.ui-dialog');
|
||||
$this->assertNotNull($button1_dialog, 'Button opens a modal dialog.');
|
||||
|
||||
$button1_dialog_content = $button1_dialog->find('css', 'div.ui-dialog-content');
|
||||
$this->assertNotNull($button1_dialog_content, 'Button opens a modal dialog.');
|
||||
|
||||
// Test the HTML escaping of & character.
|
||||
$button1_dialog_title = $button1_dialog->find('css', "span.ui-dialog-title:contains('AJAX Dialog & contents')");
|
||||
$this->assertNotNull($button1_dialog_title);
|
||||
$button1_dialog_title_amp = $button1_dialog->find('css', "span.ui-dialog-title:contains('AJAX Dialog & contents')");
|
||||
$this->assertNull($button1_dialog_title_amp);
|
||||
|
||||
// Reset: Close the dialog.
|
||||
$button1_dialog->findButton('Close')->press();
|
||||
|
||||
// Abbreviated test for "normal" dialogs, testing only the difference.
|
||||
$this->getSession()->getPage()->findButton('Button 2 (non-modal)')->press();
|
||||
$button2_dialog = $this->assertSession()->waitForElementVisible('css', 'div.ui-dialog-content');
|
||||
$this->assertNotNull($button2_dialog, 'Non-modal content displays as expected.');
|
||||
|
||||
// Use a link to close the pagnel opened by button 2.
|
||||
$this->getSession()->getPage()->clickLink('Link 4 (close non-modal if open)');
|
||||
|
||||
// Form modal.
|
||||
$this->clickLink('Link 5 (form)');
|
||||
// Two links have been clicked in succession - This time wait for a change
|
||||
// in the title as the previous closing dialog may temporarily be open.
|
||||
$form_dialog_title = $this->assertSession()->waitForElementVisible('css', "span.ui-dialog-title:contains('Ajax Form contents')");
|
||||
$this->assertNotNull($form_dialog_title, 'Dialog form has the expected title.');
|
||||
// Locate the newly opened dialog.
|
||||
$form_dialog = $this->getSession()->getPage()->find('css', 'div.ui-dialog');
|
||||
$this->assertNotNull($form_dialog, 'Form dialog is visible');
|
||||
|
||||
$form_contents = $form_dialog->find('css', "p:contains('Ajax Form contents description.')");
|
||||
$this->assertNotNull($form_contents, 'For has the expected text.');
|
||||
$do_it = $form_dialog->findButton('Do it');
|
||||
$this->assertNotNull($do_it, 'The dialog has a "Do it" button.');
|
||||
$preview = $form_dialog->findButton('Preview');
|
||||
$this->assertNotNull($preview, 'The dialog contains a "Preview" button.');
|
||||
|
||||
// Reset: close the form.
|
||||
$form_dialog->findButton('Close')->press();
|
||||
|
||||
// Non AJAX version of Link 6.
|
||||
$this->drupalGet('admin/structure/contact/add');
|
||||
// Check we get a chunk of the code, we can't test the whole form as form
|
||||
// build id and token with be different.
|
||||
$contact_form = $this->xpath("//form[@id='contact-form-add-form']");
|
||||
$this->assertTrue(!empty($contact_form), 'Non-JS entity form page present.');
|
||||
|
||||
// Reset: Return to the dialog links page.
|
||||
$this->drupalGet('ajax-test/dialog');
|
||||
|
||||
$this->clickLink('Link 6 (entity form)');
|
||||
$dialog_add = $this->assertSession()->waitForElementVisible('css', 'div.ui-dialog');
|
||||
$this->assertNotNull($dialog_add, 'Form dialog is visible');
|
||||
|
||||
$form_add = $dialog_add->find('css', 'form.contact-form-add-form');
|
||||
$this->assertNotNull($form_add, 'Modal dialog JSON contains entity form.');
|
||||
|
||||
$form_title = $dialog_add->find('css', "span.ui-dialog-title:contains('Add contact form')");
|
||||
$this->assertNotNull($form_title, 'The add form title is as expected.');
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\FunctionalJavascriptTests\Ajax;
|
||||
|
||||
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
|
||||
|
||||
/**
|
||||
* Various tests of AJAX behavior.
|
||||
*
|
||||
* @group Ajax
|
||||
*/
|
||||
class ElementValidationTest extends WebDriverTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['ajax_test', 'ajax_forms_test'];
|
||||
|
||||
/**
|
||||
* Tries to post an Ajax change to a form that has a validated element.
|
||||
*
|
||||
* Drupal AJAX commands update the DOM echoing back the validated values in
|
||||
* the form of messages that appear on the page.
|
||||
*/
|
||||
public function testAjaxElementValidation() {
|
||||
$this->drupalGet('ajax_validation_test');
|
||||
$page = $this->getSession()->getPage();
|
||||
$assert = $this->assertSession();
|
||||
|
||||
// Partially complete the form with a string.
|
||||
$page->fillField('drivertext', 'some dumb text');
|
||||
// Move focus away from this field to trigger AJAX.
|
||||
$page->findField('spare_required_field')->focus();
|
||||
|
||||
// When the AJAX command updates the DOM a <ul> unsorted list
|
||||
// "message__list" structure will appear on the page echoing back the
|
||||
// "some dumb text" message.
|
||||
$placeholder_text = $assert->waitForElement('css', "ul.messages__list li.messages__item em:contains('some dumb text')");
|
||||
$this->assertNotNull($placeholder_text, 'A callback successfully echoed back a string.');
|
||||
|
||||
$this->drupalGet('ajax_validation_test');
|
||||
// Partialy complete the form with a number.
|
||||
$page->fillField('drivernumber', '12345');
|
||||
$page->findField('spare_required_field')->focus();
|
||||
|
||||
// The AJAX request/resonse will complete successfully when a InsertCommand
|
||||
// injects a message with a placeholder element into the DOM with the
|
||||
// submitted number.
|
||||
$placeholder_number = $assert->waitForElement('css', "ul.messages__list li.messages__item em:contains('12345')");
|
||||
$this->assertNotNull($placeholder_number, 'A callback successfully echoed back a number.');
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,84 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\FunctionalJavascriptTests\Ajax;
|
||||
|
||||
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
|
||||
|
||||
/**
|
||||
* Tests that form values are properly delivered to AJAX callbacks.
|
||||
*
|
||||
* @group Ajax
|
||||
*/
|
||||
class FormValuesTest extends WebDriverTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['node', 'ajax_test', 'ajax_forms_test'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->drupalLogin($this->drupalCreateUser(['access content']));
|
||||
}
|
||||
|
||||
/**
|
||||
* Submits forms with select and checkbox elements via Ajax.
|
||||
*/
|
||||
public function testSimpleAjaxFormValue() {
|
||||
|
||||
$this->drupalGet('ajax_forms_test_get_form');
|
||||
|
||||
$session = $this->getSession();
|
||||
$assertSession = $this->assertSession();
|
||||
|
||||
// Verify form values of a select element.
|
||||
foreach (['green', 'blue', 'red'] as $item) {
|
||||
// Updating the field will trigger a AJAX request/response.
|
||||
$session->getPage()->selectFieldOption('select', $item);
|
||||
|
||||
// The AJAX command in the response will update the DOM
|
||||
$select = $assertSession->waitForElement('css', "div#ajax_selected_color:contains('$item')");
|
||||
$this->assertNotNull($select, "DataCommand has updated the page with a value of $item.");
|
||||
}
|
||||
|
||||
// Verify form values of a checkbox element.
|
||||
$session->getPage()->checkField('checkbox');
|
||||
$div0 = $this->assertSession()->waitForElement('css', "div#ajax_checkbox_value:contains('checked')");
|
||||
$this->assertNotNull($div0, 'DataCommand updates the DOM as expected when a checkbox is selected');
|
||||
|
||||
$session->getPage()->uncheckField('checkbox');
|
||||
$div1 = $this->assertSession()->waitForElement('css', "div#ajax_checkbox_value:contains('unchecked')");
|
||||
$this->assertNotNull($div1, 'DataCommand updates the DOM as expected when a checkbox is de-selected');
|
||||
|
||||
// Verify that AJAX elements with invalid callbacks return error code 500.
|
||||
// Ensure the test error log is empty before these tests.
|
||||
$this->assertFalse(file_exists(DRUPAL_ROOT . '/' . $this->siteDirectory . '/error.log'), 'PHP error.log is empty.');
|
||||
// We don't need to check for the X-Drupal-Ajax-Token header with these
|
||||
// invalid requests.
|
||||
$this->assertAjaxHeader = FALSE;
|
||||
foreach (['null', 'empty', 'nonexistent'] as $key) {
|
||||
$element_name = 'select_' . $key . '_callback';
|
||||
// Updating the field will trigger a AJAX request/response.
|
||||
$session->getPage()->selectFieldOption($element_name, 'green');
|
||||
|
||||
// The select element is disabled as the AJAX request is issued.
|
||||
$this->assertSession()->waitForElement('css', "select[name=\"$element_name\"]:disabled");
|
||||
|
||||
// The select element is enabled as the response is receieved.
|
||||
$this->assertSession()->waitForElement('css', "select[name=\"$element_name\"]:enabled");
|
||||
$this->assertTrue(file_exists(DRUPAL_ROOT . '/' . $this->siteDirectory . '/error.log'), 'PHP error.log is not empty.');
|
||||
$this->assertContains('"The specified #ajax callback is empty or not callable."', file_get_contents(DRUPAL_ROOT . '/' . $this->siteDirectory . '/error.log'));
|
||||
// The exceptions are expected. Do not interpret them as a test failure.
|
||||
// Not using File API; a potential error must trigger a PHP warning.
|
||||
unlink(\Drupal::root() . '/' . $this->siteDirectory . '/error.log');
|
||||
}
|
||||
// We need to reload the page to kill any unfinished AJAX calls before
|
||||
// tearDown() is called.
|
||||
$this->drupalGet('ajax_forms_test_get_form');
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,139 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\FunctionalJavascriptTests\Ajax;
|
||||
|
||||
use Drupal\Component\Render\FormattableMarkup;
|
||||
use Drupal\Core\Field\FieldStorageDefinitionInterface;
|
||||
use Drupal\field\Entity\FieldConfig;
|
||||
use Drupal\field\Entity\FieldStorageConfig;
|
||||
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
|
||||
|
||||
/**
|
||||
* Tests that AJAX-enabled forms work when multiple instances of the same form
|
||||
* are on a page.
|
||||
*
|
||||
* @group Ajax
|
||||
*/
|
||||
class MultiFormTest extends WebDriverTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['node', 'form_test'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->drupalCreateContentType(['type' => 'page', 'name' => 'Page']);
|
||||
|
||||
// Create a multi-valued field for 'page' nodes to use for Ajax testing.
|
||||
$field_name = 'field_ajax_test';
|
||||
FieldStorageConfig::create([
|
||||
'entity_type' => 'node',
|
||||
'field_name' => $field_name,
|
||||
'type' => 'text',
|
||||
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
|
||||
])->save();
|
||||
FieldConfig::create([
|
||||
'field_name' => $field_name,
|
||||
'entity_type' => 'node',
|
||||
'bundle' => 'page',
|
||||
])->save();
|
||||
entity_get_form_display('node', 'page', 'default')
|
||||
->setComponent($field_name, ['type' => 'text_textfield'])
|
||||
->save();
|
||||
|
||||
// Log in a user who can create 'page' nodes.
|
||||
$this->drupalLogin($this->drupalCreateUser(['create page content']));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that pages with the 'node_page_form' included twice work correctly.
|
||||
*/
|
||||
public function testMultiForm() {
|
||||
// HTML IDs for elements within the field are potentially modified with
|
||||
// each Ajax submission, but these variables are stable and help target the
|
||||
// desired elements.
|
||||
$field_name = 'field_ajax_test';
|
||||
|
||||
$form_xpath = '//form[starts-with(@id, "node-page-form")]';
|
||||
$field_xpath = '//div[contains(@class, "field--name-field-ajax-test")]';
|
||||
$button_name = $field_name . '_add_more';
|
||||
$button_value = t('Add another item');
|
||||
$button_xpath_suffix = '//input[@name="' . $button_name . '"]';
|
||||
$field_items_xpath_suffix = '//input[@type="text"]';
|
||||
|
||||
// Ensure the initial page contains both node forms and the correct number
|
||||
// of field items and "add more" button for the multi-valued field within
|
||||
// each form.
|
||||
$this->drupalGet('form-test/two-instances-of-same-form');
|
||||
|
||||
// Wait for javascript on the page to prepare the form attributes.
|
||||
$this->assertSession()->assertWaitOnAjaxRequest();
|
||||
|
||||
$session = $this->getSession();
|
||||
$page = $session->getPage();
|
||||
$fields = $page->findAll('xpath', $form_xpath . $field_xpath);
|
||||
$this->assertEqual(count($fields), 2);
|
||||
foreach ($fields as $field) {
|
||||
$this->assertCount(1, $field->findAll('xpath', '.' . $field_items_xpath_suffix), 'Found the correct number of field items on the initial page.');
|
||||
$this->assertFieldsByValue($field->find('xpath', '.' . $button_xpath_suffix), NULL, 'Found the "add more" button on the initial page.');
|
||||
}
|
||||
|
||||
$this->assertNoDuplicateIds();
|
||||
|
||||
// Submit the "add more" button of each form twice. After each corresponding
|
||||
// page update, ensure the same as above.
|
||||
|
||||
for ($i = 0; $i < 2; $i++) {
|
||||
$forms = $page->find('xpath', $form_xpath);
|
||||
foreach ($forms as $offset => $form) {
|
||||
$button = $form->findButton($button_value);
|
||||
$this->assertNotNull($button, 'Add Another Item button exists');
|
||||
$button->press();
|
||||
|
||||
// Wait for page update.
|
||||
$this->assertSession()->assertWaitOnAjaxRequest();
|
||||
|
||||
// After AJAX request and response page will update.
|
||||
$page_updated = $session->getPage();
|
||||
$field = $page_updated->findAll('xpath', '.' . $field_xpath);
|
||||
$this->assertEqual(count($field[0]->find('xpath', '.' . $field_items_xpath_suffix)), $i + 2, 'Found the correct number of field items after an AJAX submission.');
|
||||
$this->assertFieldsByValue($field[0]->find('xpath', '.' . $button_xpath_suffix), NULL, 'Found the "add more" button after an AJAX submission.');
|
||||
$this->assertNoDuplicateIds();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that each HTML ID is used for just a single element on the page.
|
||||
*
|
||||
* @param string $message
|
||||
* (optional) A message to display with the assertion.
|
||||
*/
|
||||
protected function assertNoDuplicateIds($message = '') {
|
||||
$args = ['@url' => $this->getUrl()];
|
||||
|
||||
if (!$elements = $this->xpath('//*[@id]')) {
|
||||
$this->fail(new FormattableMarkup('The page @url contains no HTML IDs.', $args));
|
||||
return;
|
||||
}
|
||||
|
||||
$message = $message ?: new FormattableMarkup('The page @url does not contain duplicate HTML IDs', $args);
|
||||
|
||||
$seen_ids = [];
|
||||
foreach ($elements as $element) {
|
||||
$id = $element->getAttribute('id');
|
||||
if (isset($seen_ids[$id])) {
|
||||
$this->fail($message);
|
||||
return;
|
||||
}
|
||||
$seen_ids[$id] = TRUE;
|
||||
}
|
||||
$this->assertTrue(TRUE, $message);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,106 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\FunctionalJavascriptTests\Ajax;
|
||||
|
||||
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
|
||||
|
||||
/**
|
||||
* Tests the throbber.
|
||||
*
|
||||
* @group Ajax
|
||||
*/
|
||||
class ThrobberTest extends WebDriverTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = [
|
||||
'node',
|
||||
'views',
|
||||
'views_ui',
|
||||
'views_ui_test_field',
|
||||
'hold_test',
|
||||
];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$admin_user = $this->drupalCreateUser([
|
||||
'administer views',
|
||||
]);
|
||||
$this->drupalLogin($admin_user);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests theming throbber element.
|
||||
*/
|
||||
public function testThemingThrobberElement() {
|
||||
$session = $this->getSession();
|
||||
$web_assert = $this->assertSession();
|
||||
$page = $session->getPage();
|
||||
|
||||
$custom_ajax_progress_indicator_fullscreen = <<<JS
|
||||
Drupal.theme.ajaxProgressIndicatorFullscreen = function () {
|
||||
return '<div class="custom-ajax-progress-fullscreen"></div>';
|
||||
};
|
||||
JS;
|
||||
$custom_ajax_progress_throbber = <<<JS
|
||||
Drupal.theme.ajaxProgressThrobber = function (message) {
|
||||
return '<div class="custom-ajax-progress-throbber"></div>';
|
||||
};
|
||||
JS;
|
||||
$custom_ajax_progress_message = <<<JS
|
||||
Drupal.theme.ajaxProgressMessage = function (message) {
|
||||
return '<div class="custom-ajax-progress-message">Hold door!</div>';
|
||||
};
|
||||
JS;
|
||||
|
||||
$this->drupalGet('admin/structure/views/view/content');
|
||||
$this->waitForNoElement('.ajax-progress-fullscreen');
|
||||
|
||||
// Test theming fullscreen throbber.
|
||||
$session->executeScript($custom_ajax_progress_indicator_fullscreen);
|
||||
hold_test_response(TRUE);
|
||||
$page->clickLink('Content: Published (grouped)');
|
||||
$this->assertNotNull($web_assert->waitForElement('css', '.custom-ajax-progress-fullscreen'), 'Custom ajaxProgressIndicatorFullscreen.');
|
||||
hold_test_response(FALSE);
|
||||
$this->waitForNoElement('.custom-ajax-progress-fullscreen');
|
||||
|
||||
// Test theming throbber message.
|
||||
$web_assert->waitForElementVisible('css', '[data-drupal-selector="edit-options-group-info-add-group"]');
|
||||
$session->executeScript($custom_ajax_progress_message);
|
||||
hold_test_response(TRUE);
|
||||
$page->pressButton('Add another item');
|
||||
$this->assertNotNull($web_assert->waitForElement('css', '.ajax-progress-throbber .custom-ajax-progress-message'), 'Custom ajaxProgressMessage.');
|
||||
hold_test_response(FALSE);
|
||||
$this->waitForNoElement('.ajax-progress-throbber');
|
||||
|
||||
// Test theming throbber.
|
||||
$web_assert->waitForElementVisible('css', '[data-drupal-selector="edit-options-group-info-group-items-3-title"]');
|
||||
$session->executeScript($custom_ajax_progress_throbber);
|
||||
hold_test_response(TRUE);
|
||||
$page->pressButton('Add another item');
|
||||
$this->assertNotNull($web_assert->waitForElement('css', '.custom-ajax-progress-throbber'), 'Custom ajaxProgressThrobber.');
|
||||
hold_test_response(FALSE);
|
||||
$this->waitForNoElement('.custom-ajax-progress-throbber');
|
||||
}
|
||||
|
||||
/**
|
||||
* Waits for an element to be removed from the page.
|
||||
*
|
||||
* @param string $selector
|
||||
* CSS selector.
|
||||
* @param int $timeout
|
||||
* (optional) Timeout in milliseconds, defaults to 10000.
|
||||
*
|
||||
* @todo Remove in https://www.drupal.org/node/2892440.
|
||||
*/
|
||||
protected function waitForNoElement($selector, $timeout = 10000) {
|
||||
$condition = "(typeof jQuery !== 'undefined' && jQuery('$selector').length === 0)";
|
||||
$this->assertJsCondition($condition, $timeout);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,63 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\FunctionalJavascriptTests;
|
||||
|
||||
/**
|
||||
* Tests if we can execute JavaScript in the browser.
|
||||
*
|
||||
* @group javascript
|
||||
*/
|
||||
class BrowserWithJavascriptTest extends WebDriverTestBase {
|
||||
|
||||
public function testJavascript() {
|
||||
$this->drupalGet('<front>');
|
||||
$session = $this->getSession();
|
||||
|
||||
$session->resizeWindow(400, 300);
|
||||
$javascript = <<<JS
|
||||
(function(){
|
||||
var w = window,
|
||||
d = document,
|
||||
e = d.documentElement,
|
||||
g = d.getElementsByTagName('body')[0],
|
||||
x = w.innerWidth || e.clientWidth || g.clientWidth,
|
||||
y = w.innerHeight || e.clientHeight|| g.clientHeight;
|
||||
return x == 400 && y == 300;
|
||||
}());
|
||||
JS;
|
||||
$this->assertJsCondition($javascript);
|
||||
}
|
||||
|
||||
public function testAssertJsCondition() {
|
||||
$this->drupalGet('<front>');
|
||||
$session = $this->getSession();
|
||||
|
||||
$session->resizeWindow(500, 300);
|
||||
$javascript = <<<JS
|
||||
(function(){
|
||||
var w = window,
|
||||
d = document,
|
||||
e = d.documentElement,
|
||||
g = d.getElementsByTagName('body')[0],
|
||||
x = w.innerWidth || e.clientWidth || g.clientWidth,
|
||||
y = w.innerHeight || e.clientHeight|| g.clientHeight;
|
||||
return x == 400 && y == 300;
|
||||
}());
|
||||
JS;
|
||||
|
||||
// We expected the following assertion to fail because the window has been
|
||||
// re-sized to have a width of 500 not 400.
|
||||
$this->setExpectedException(\PHPUnit_Framework_AssertionFailedError::class);
|
||||
$this->assertJsCondition($javascript, 100);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests creating screenshots.
|
||||
*/
|
||||
public function testCreateScreenshot() {
|
||||
$this->drupalGet('<front>');
|
||||
$this->createScreenshot('public://screenshot.jpg');
|
||||
$this->assertFileExists('public://screenshot.jpg');
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,133 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\FunctionalJavascriptTests\Core\Form;
|
||||
|
||||
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
|
||||
|
||||
/**
|
||||
* Tests for form grouping elements.
|
||||
*
|
||||
* @group form
|
||||
*/
|
||||
class FormGroupingElementsTest extends WebDriverTestBase {
|
||||
|
||||
/**
|
||||
* Required modules.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = ['form_test'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$account = $this->drupalCreateUser();
|
||||
$this->drupalLogin($account);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that vertical tab children become visible.
|
||||
*
|
||||
* Makes sure that a child element of a vertical tab that is not visible,
|
||||
* becomes visible when the tab is clicked, a fragment link to the child is
|
||||
* clicked or when the URI fragment pointing to that child changes.
|
||||
*/
|
||||
public function testVerticalTabChildVisibility() {
|
||||
$session = $this->getSession();
|
||||
$web_assert = $this->assertSession();
|
||||
|
||||
// Request the group vertical tabs testing page with a fragment identifier
|
||||
// to the second element.
|
||||
$this->drupalGet('form-test/group-vertical-tabs', ['fragment' => 'edit-element-2']);
|
||||
|
||||
$page = $session->getPage();
|
||||
|
||||
$tab_link_1 = $page->find('css', '.vertical-tabs__menu-item > a');
|
||||
|
||||
$child_1_selector = '#edit-element';
|
||||
$child_1 = $page->find('css', $child_1_selector);
|
||||
|
||||
$child_2_selector = '#edit-element-2';
|
||||
$child_2 = $page->find('css', $child_2_selector);
|
||||
|
||||
// Assert that the child in the second vertical tab becomes visible.
|
||||
// It should be visible after initial load due to the fragment in the URI.
|
||||
$this->assertTrue($child_2->isVisible(), 'Child 2 is visible due to a URI fragment');
|
||||
|
||||
// Click on a fragment link pointing to an invisible child inside an
|
||||
// inactive vertical tab.
|
||||
$session->executeScript("jQuery('<a href=\"$child_1_selector\"></a>').insertAfter('h1')[0].click()");
|
||||
|
||||
// Assert that the child in the first vertical tab becomes visible.
|
||||
$web_assert->waitForElementVisible('css', $child_1_selector, 50);
|
||||
|
||||
// Trigger a URI fragment change (hashchange) to show the second vertical
|
||||
// tab again.
|
||||
$session->executeScript("location.replace('$child_2_selector')");
|
||||
|
||||
// Assert that the child in the second vertical tab becomes visible again.
|
||||
$web_assert->waitForElementVisible('css', $child_2_selector, 50);
|
||||
|
||||
$tab_link_1->click();
|
||||
|
||||
// Assert that the child in the first vertical tab is visible again after
|
||||
// a click on the first tab.
|
||||
$this->assertTrue($child_1->isVisible(), 'Child 1 is visible after clicking the parent tab');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that details element children become visible.
|
||||
*
|
||||
* Makes sure that a child element of a details element that is not visible,
|
||||
* becomes visible when a fragment link to the child is clicked or when the
|
||||
* URI fragment pointing to that child changes.
|
||||
*/
|
||||
public function testDetailsChildVisibility() {
|
||||
$session = $this->getSession();
|
||||
$web_assert = $this->assertSession();
|
||||
|
||||
// Store reusable JavaScript code to remove the current URI fragment and
|
||||
// close all details.
|
||||
$reset_js = "location.replace('#'); jQuery('details').removeAttr('open')";
|
||||
|
||||
// Request the group details testing page.
|
||||
$this->drupalGet('form-test/group-details');
|
||||
|
||||
$page = $session->getPage();
|
||||
|
||||
$session->executeScript($reset_js);
|
||||
|
||||
$child_selector = '#edit-element';
|
||||
$child = $page->find('css', $child_selector);
|
||||
|
||||
// Assert that the child is not visible.
|
||||
$this->assertFalse($child->isVisible(), 'Child is not visible');
|
||||
|
||||
// Trigger a URI fragment change (hashchange) to open all parent details
|
||||
// elements of the child.
|
||||
$session->executeScript("location.replace('$child_selector')");
|
||||
|
||||
// Assert that the child becomes visible again after a hash change.
|
||||
$web_assert->waitForElementVisible('css', $child_selector, 50);
|
||||
|
||||
$session->executeScript($reset_js);
|
||||
|
||||
// Click on a fragment link pointing to an invisible child inside a closed
|
||||
// details element.
|
||||
$session->executeScript("jQuery('<a href=\"$child_selector\"></a>').insertAfter('h1')[0].click()");
|
||||
|
||||
// Assert that the child is visible again after a fragment link click.
|
||||
$web_assert->waitForElementVisible('css', $child_selector, 50);
|
||||
|
||||
// Find the summary belonging to the closest details element.
|
||||
$summary = $page->find('css', '#edit-meta > summary');
|
||||
|
||||
// Assert that both aria-expanded and aria-pressed are true.
|
||||
$this->assertTrue($summary->getAttribute('aria-expanded'));
|
||||
$this->assertTrue($summary->getAttribute('aria-pressed'));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,132 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\FunctionalJavascriptTests\Core\Installer\Form;
|
||||
|
||||
use Drupal\Core\DependencyInjection\ContainerBuilder;
|
||||
use Drupal\Core\Language\Language;
|
||||
use Drupal\Core\Session\UserSession;
|
||||
use Drupal\Core\Test\HttpClientMiddleware\TestHttpClientMiddleware;
|
||||
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
|
||||
use GuzzleHttp\HandlerStack;
|
||||
use Symfony\Component\DependencyInjection\Reference;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
|
||||
/**
|
||||
* Tests the select profile form.
|
||||
*
|
||||
* @group Installer
|
||||
*/
|
||||
class SelectProfileFormTest extends WebDriverTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setUp() {
|
||||
$this->setupBaseUrl();
|
||||
|
||||
$this->prepareDatabasePrefix();
|
||||
|
||||
// Install Drupal test site.
|
||||
$this->prepareEnvironment();
|
||||
|
||||
// Define information about the user 1 account.
|
||||
$this->rootUser = new UserSession([
|
||||
'uid' => 1,
|
||||
'name' => 'admin',
|
||||
'mail' => 'admin@example.com',
|
||||
'pass_raw' => $this->randomMachineName(),
|
||||
]);
|
||||
|
||||
// If any $settings are defined for this test, copy and prepare an actual
|
||||
// settings.php, so as to resemble a regular installation.
|
||||
if (!empty($this->settings)) {
|
||||
// Not using File API; a potential error must trigger a PHP warning.
|
||||
copy(DRUPAL_ROOT . '/sites/default/default.settings.php', DRUPAL_ROOT . '/' . $this->siteDirectory . '/settings.php');
|
||||
$this->writeSettings($this->settings);
|
||||
}
|
||||
|
||||
// Note that FunctionalTestSetupTrait::installParameters() returns form
|
||||
// input values suitable for a programmed
|
||||
// \Drupal::formBuilder()->submitForm().
|
||||
// @see InstallerTestBase::translatePostValues()
|
||||
$this->parameters = $this->installParameters();
|
||||
|
||||
// Set up a minimal container (required by BrowserTestBase). Set cookie and
|
||||
// server information so that XDebug works.
|
||||
// @see install_begin_request()
|
||||
$request = Request::create($GLOBALS['base_url'] . '/core/install.php', 'GET', [], $_COOKIE, [], $_SERVER);
|
||||
$this->container = new ContainerBuilder();
|
||||
$request_stack = new RequestStack();
|
||||
$request_stack->push($request);
|
||||
$this->container
|
||||
->set('request_stack', $request_stack);
|
||||
$this->container
|
||||
->setParameter('language.default_values', Language::$defaultValues);
|
||||
$this->container
|
||||
->register('language.default', 'Drupal\Core\Language\LanguageDefault')
|
||||
->addArgument('%language.default_values%');
|
||||
$this->container
|
||||
->register('string_translation', 'Drupal\Core\StringTranslation\TranslationManager')
|
||||
->addArgument(new Reference('language.default'));
|
||||
$this->container
|
||||
->register('http_client', 'GuzzleHttp\Client')
|
||||
->setFactory('http_client_factory:fromOptions');
|
||||
$this->container
|
||||
->register('http_client_factory', 'Drupal\Core\Http\ClientFactory')
|
||||
->setArguments([new Reference('http_handler_stack')]);
|
||||
$handler_stack = HandlerStack::create();
|
||||
$test_http_client_middleware = new TestHttpClientMiddleware();
|
||||
$handler_stack->push($test_http_client_middleware(), 'test.http_client.middleware');
|
||||
$this->container
|
||||
->set('http_handler_stack', $handler_stack);
|
||||
|
||||
$this->container
|
||||
->set('app.root', DRUPAL_ROOT);
|
||||
\Drupal::setContainer($this->container);
|
||||
|
||||
// Setup Mink.
|
||||
$this->initMink();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function initMink() {
|
||||
// The temporary files directory doesn't exist yet, as install_base_system()
|
||||
// has not run. We need to create the template cache directory recursively.
|
||||
$path = $this->tempFilesDirectory . DIRECTORY_SEPARATOR . 'browsertestbase-templatecache';
|
||||
if (!file_exists($path)) {
|
||||
mkdir($path, 0777, TRUE);
|
||||
}
|
||||
|
||||
parent::initMink();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* BrowserTestBase::refreshVariables() tries to operate on persistent storage,
|
||||
* which is only available after the installer completed.
|
||||
*/
|
||||
protected function refreshVariables() {
|
||||
// Intentionally empty as the site is not yet installed.
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests a warning message is displayed when the Umami profile is selected.
|
||||
*/
|
||||
public function testUmamiProfileWarningMessage() {
|
||||
$this->drupalGet($GLOBALS['base_url'] . '/core/install.php');
|
||||
$edit = [
|
||||
'langcode' => 'en',
|
||||
];
|
||||
$this->drupalPostForm(NULL, $edit, 'Save and continue');
|
||||
$page = $this->getSession()->getPage();
|
||||
$warning_message = $page->find('css', '.description .messages--warning');
|
||||
$this->assertFalse($warning_message->isVisible());
|
||||
$page->selectFieldOption('profile', 'demo_umami');
|
||||
$this->assertTrue($warning_message->isVisible());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,145 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\FunctionalJavascriptTests\Core;
|
||||
|
||||
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
|
||||
|
||||
/**
|
||||
* Tests for the machine name field.
|
||||
*
|
||||
* @group field
|
||||
*/
|
||||
class MachineNameTest extends WebDriverTestBase {
|
||||
|
||||
/**
|
||||
* Required modules.
|
||||
*
|
||||
* Node is required because the machine name callback checks for
|
||||
* access_content.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = ['node', 'form_test'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$account = $this->drupalCreateUser([
|
||||
'access content',
|
||||
]);
|
||||
$this->drupalLogin($account);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that machine name field functions.
|
||||
*
|
||||
* Makes sure that the machine name field automatically provides a valid
|
||||
* machine name and that the manual editing mode functions.
|
||||
*/
|
||||
public function testMachineName() {
|
||||
// Visit the machine name test page which contains two machine name fields.
|
||||
$this->drupalGet('form-test/machine-name');
|
||||
|
||||
// Test values for conversion.
|
||||
$test_values = [
|
||||
[
|
||||
'input' => 'Test value !0-9@',
|
||||
'message' => 'A title that should be transliterated must be equal to the php generated machine name',
|
||||
'expected' => 'test_value_0_9_',
|
||||
],
|
||||
[
|
||||
'input' => 'Test value',
|
||||
'message' => 'A title that should not be transliterated must be equal to the php generated machine name',
|
||||
'expected' => 'test_value',
|
||||
],
|
||||
];
|
||||
|
||||
// Get page and session.
|
||||
$page = $this->getSession()->getPage();
|
||||
|
||||
// Get elements from the page.
|
||||
$title_1 = $page->findField('machine_name_1_label');
|
||||
$machine_name_1_field = $page->findField('machine_name_1');
|
||||
$machine_name_2_field = $page->findField('machine_name_2');
|
||||
$machine_name_1_wrapper = $machine_name_1_field->getParent();
|
||||
$machine_name_2_wrapper = $machine_name_2_field->getParent();
|
||||
$machine_name_1_value = $page->find('css', '#edit-machine-name-1-label-machine-name-suffix .machine-name-value');
|
||||
$machine_name_2_value = $page->find('css', '#edit-machine-name-2-label-machine-name-suffix .machine-name-value');
|
||||
$button_1 = $page->find('css', '#edit-machine-name-1-label-machine-name-suffix button.link');
|
||||
|
||||
// Assert both fields are initialized correctly.
|
||||
$this->assertNotEmpty($machine_name_1_value, 'Machine name field 1 must be initialized');
|
||||
$this->assertNotEmpty($machine_name_2_value, 'Machine name field 2 must be initialized');
|
||||
|
||||
// Field must be present for the rest of the test to work.
|
||||
if (empty($machine_name_1_value)) {
|
||||
$this->fail('Cannot finish test, missing machine name field');
|
||||
}
|
||||
|
||||
// Test each value for conversion to a machine name.
|
||||
foreach ($test_values as $test_info) {
|
||||
// Set the value for the field, triggering the machine name update.
|
||||
$title_1->setValue($test_info['input']);
|
||||
|
||||
// Wait the set timeout for fetching the machine name.
|
||||
$this->assertJsCondition('jQuery("#edit-machine-name-1-label-machine-name-suffix .machine-name-value").html() == "' . $test_info['expected'] . '"');
|
||||
|
||||
// Validate the generated machine name.
|
||||
$this->assertEquals($test_info['expected'], $machine_name_1_value->getHtml(), $test_info['message']);
|
||||
|
||||
// Validate the second machine name field is empty.
|
||||
$this->assertEmpty($machine_name_2_value->getHtml(), 'The second machine name field should still be empty');
|
||||
}
|
||||
|
||||
// Validate the machine name field is hidden. Elements are visually hidden
|
||||
// using positioning, isVisible() will therefore not work.
|
||||
$this->assertEquals(TRUE, $machine_name_1_wrapper->hasClass('visually-hidden'), 'The ID field must not be visible');
|
||||
$this->assertEquals(TRUE, $machine_name_2_wrapper->hasClass('visually-hidden'), 'The ID field must not be visible');
|
||||
|
||||
// Test switching back to the manual editing mode by clicking the edit link.
|
||||
$button_1->click();
|
||||
|
||||
// Validate the visibility of the machine name field.
|
||||
$this->assertEquals(FALSE, $machine_name_1_wrapper->hasClass('visually-hidden'), 'The ID field must now be visible');
|
||||
|
||||
// Validate the visibility of the second machine name field.
|
||||
$this->assertEquals(TRUE, $machine_name_2_wrapper->hasClass('visually-hidden'), 'The ID field must not be visible');
|
||||
|
||||
// Validate if the element contains the correct value.
|
||||
$this->assertEquals($test_values[1]['expected'], $machine_name_1_field->getValue(), 'The ID field value must be equal to the php generated machine name');
|
||||
|
||||
$assert = $this->assertSession();
|
||||
$this->drupalGet('/form-test/form-test-machine-name-validation');
|
||||
|
||||
// Test errors after with no AJAX.
|
||||
$assert->buttonExists('Save')->press();
|
||||
$assert->pageTextContains('Machine-readable name field is required.');
|
||||
// Ensure only the first machine name field has an error.
|
||||
$this->assertTrue($assert->fieldExists('id')->hasClass('error'));
|
||||
$this->assertFalse($assert->fieldExists('id2')->hasClass('error'));
|
||||
|
||||
// Test a successful submit after using AJAX.
|
||||
$assert->fieldExists('Name')->setValue('test 1');
|
||||
$assert->fieldExists('id')->setValue('test_1');
|
||||
$assert->selectExists('snack')->selectOption('apple');
|
||||
$assert->assertWaitOnAjaxRequest();
|
||||
$assert->buttonExists('Save')->press();
|
||||
$assert->pageTextContains('The form_test_machine_name_validation_form form has been submitted successfully.');
|
||||
|
||||
// Test errors after using AJAX.
|
||||
$assert->fieldExists('Name')->setValue('duplicate');
|
||||
$this->assertJsCondition('document.forms[0].id.value === "duplicate"');
|
||||
$assert->fieldExists('id2')->setValue('duplicate2');
|
||||
$assert->selectExists('snack')->selectOption('potato');
|
||||
$assert->assertWaitOnAjaxRequest();
|
||||
$assert->buttonExists('Save')->press();
|
||||
$assert->pageTextContains('The machine-readable name is already in use. It must be unique.');
|
||||
// Ensure both machine name fields both have errors.
|
||||
$this->assertTrue($assert->fieldExists('id')->hasClass('error'));
|
||||
$this->assertTrue($assert->fieldExists('id2')->hasClass('error'));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,58 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\FunctionalJavascriptTests\Core\Session;
|
||||
|
||||
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
|
||||
use Drupal\menu_link_content\Entity\MenuLinkContent;
|
||||
|
||||
/**
|
||||
* Tests that sessions don't expire.
|
||||
*
|
||||
* @group session
|
||||
*/
|
||||
class SessionTest extends WebDriverTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['menu_link_content', 'block'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$account = $this->drupalCreateUser();
|
||||
$this->drupalLogin($account);
|
||||
|
||||
$menu_link_content = MenuLinkContent::create([
|
||||
'title' => 'Link to front page',
|
||||
'menu_name' => 'tools',
|
||||
'link' => ['uri' => 'route:<front>'],
|
||||
]);
|
||||
$menu_link_content->save();
|
||||
|
||||
$this->drupalPlaceBlock('system_menu_block:tools');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that the session doesn't expire.
|
||||
*
|
||||
* Makes sure that drupal_valid_test_ua() works for multiple requests
|
||||
* performed by the Mink browser. The SIMPLETEST_USER_AGENT cookie must always
|
||||
* be valid.
|
||||
*/
|
||||
public function testSessionExpiration() {
|
||||
// Visit the front page and click the link back to the front page a large
|
||||
// number of times.
|
||||
$this->drupalGet('<front>');
|
||||
|
||||
$page = $this->getSession()->getPage();
|
||||
|
||||
for ($i = 0; $i < 25; $i++) {
|
||||
$page->clickLink('Link to front page');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\FunctionalJavascriptTests\Dialog;
|
||||
|
||||
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
|
||||
|
||||
/**
|
||||
* Tests the JavaScript functionality of the dialog position.
|
||||
*
|
||||
* @group dialog
|
||||
*/
|
||||
class DialogPositionTest extends WebDriverTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['block'];
|
||||
|
||||
/**
|
||||
* Tests if the dialog UI works properly with block layout page.
|
||||
*/
|
||||
public function testDialogOpenAndClose() {
|
||||
$admin_user = $this->drupalCreateUser(['administer blocks']);
|
||||
$this->drupalLogin($admin_user);
|
||||
$this->drupalGet('admin/structure/block');
|
||||
$session = $this->getSession();
|
||||
$assert_session = $this->assertSession();
|
||||
$page = $session->getPage();
|
||||
|
||||
// Open the dialog using the place block link.
|
||||
$placeBlockLink = $page->findLink('Place block');
|
||||
$this->assertTrue($placeBlockLink->isVisible(), 'Place block button exists.');
|
||||
$placeBlockLink->click();
|
||||
$assert_session->assertWaitOnAjaxRequest();
|
||||
$dialog = $page->find('css', '.ui-dialog');
|
||||
$this->assertTrue($dialog->isVisible(), 'Dialog is opened after clicking the Place block button.');
|
||||
|
||||
// Close the dialog again.
|
||||
$closeButton = $page->find('css', '.ui-dialog-titlebar-close');
|
||||
$closeButton->click();
|
||||
$assert_session->assertWaitOnAjaxRequest();
|
||||
$dialog = $page->find('css', '.ui-dialog');
|
||||
$this->assertNull($dialog, 'Dialog is closed after clicking the close button.');
|
||||
|
||||
// Resize the window. The test should pass after waiting for Javascript to
|
||||
// finish as no Javascript errors should have been triggered. If there were
|
||||
// javascript errors the test will fail on that.
|
||||
$session->resizeWindow(625, 625);
|
||||
$assert_session->assertWaitOnAjaxRequest();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\FunctionalJavascriptTests;
|
||||
|
||||
use Behat\Mink\Driver\Selenium2Driver;
|
||||
use WebDriver\ServiceFactory;
|
||||
|
||||
/**
|
||||
* Provides a driver for Selenium testing.
|
||||
*/
|
||||
class DrupalSelenium2Driver extends Selenium2Driver {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function __construct($browserName = 'firefox', $desiredCapabilities = NULL, $wdHost = 'http://localhost:4444/wd/hub') {
|
||||
parent::__construct($browserName, $desiredCapabilities, $wdHost);
|
||||
ServiceFactory::getInstance()->setServiceClass('service.curl', WebDriverCurlService::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setCookie($name, $value = NULL) {
|
||||
if ($value === NULL) {
|
||||
$this->getWebDriverSession()->deleteCookie($name);
|
||||
return;
|
||||
}
|
||||
|
||||
$cookieArray = [
|
||||
'name' => $name,
|
||||
'value' => urlencode($value),
|
||||
'secure' => FALSE,
|
||||
// Unlike \Behat\Mink\Driver\Selenium2Driver::setCookie we set a domain
|
||||
// and an expire date, as otherwise cookies leak from one test site into
|
||||
// another.
|
||||
'domain' => parse_url($this->getWebDriverSession()->url(), PHP_URL_HOST),
|
||||
'expires' => time() + 80000,
|
||||
];
|
||||
|
||||
$this->getWebDriverSession()->setCookie($cookieArray);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,102 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\FunctionalJavascriptTests\EntityReference;
|
||||
|
||||
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
|
||||
use Drupal\simpletest\ContentTypeCreationTrait;
|
||||
use Drupal\simpletest\NodeCreationTrait;
|
||||
use Drupal\Tests\field\Traits\EntityReferenceTestTrait;
|
||||
|
||||
/**
|
||||
* Tests the output of entity reference autocomplete widgets.
|
||||
*
|
||||
* @group entity_reference
|
||||
*/
|
||||
class EntityReferenceAutocompleteWidgetTest extends WebDriverTestBase {
|
||||
|
||||
use ContentTypeCreationTrait;
|
||||
use EntityReferenceTestTrait;
|
||||
use NodeCreationTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['node'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
// Create a Content type and two test nodes.
|
||||
$this->createContentType(['type' => 'page']);
|
||||
$this->createNode(['title' => 'Test page']);
|
||||
$this->createNode(['title' => 'Page test']);
|
||||
|
||||
$user = $this->drupalCreateUser([
|
||||
'access content',
|
||||
'create page content',
|
||||
]);
|
||||
$this->drupalLogin($user);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that the default autocomplete widget return the correct results.
|
||||
*/
|
||||
public function testEntityReferenceAutocompleteWidget() {
|
||||
// Create an entity reference field and use the default 'CONTAINS' match
|
||||
// operator.
|
||||
$field_name = 'field_test';
|
||||
$this->createEntityReferenceField('node', 'page', $field_name, $field_name, 'node', 'default', ['target_bundles' => ['page']]);
|
||||
entity_get_form_display('node', 'page', 'default')
|
||||
->setComponent($field_name, [
|
||||
'type' => 'entity_reference_autocomplete',
|
||||
'settings' => [
|
||||
'match_operator' => 'CONTAINS',
|
||||
],
|
||||
])
|
||||
->save();
|
||||
|
||||
// Visit the node add page.
|
||||
$this->drupalGet('node/add/page');
|
||||
$page = $this->getSession()->getPage();
|
||||
$assert_session = $this->assertSession();
|
||||
|
||||
$autocomplete_field = $assert_session->waitForElement('css', '[name="' . $field_name . '[0][target_id]"].ui-autocomplete-input');
|
||||
$autocomplete_field->setValue('Test');
|
||||
$this->getSession()->getDriver()->keyDown($autocomplete_field->getXpath(), ' ');
|
||||
$assert_session->waitOnAutocomplete();
|
||||
|
||||
$results = $page->findAll('css', '.ui-autocomplete li');
|
||||
|
||||
$this->assertCount(2, $results);
|
||||
$assert_session->pageTextContains('Test page');
|
||||
$assert_session->pageTextContains('Page test');
|
||||
|
||||
// Now switch the autocomplete widget to the 'STARTS_WITH' match operator.
|
||||
entity_get_form_display('node', 'page', 'default')
|
||||
->setComponent($field_name, [
|
||||
'type' => 'entity_reference_autocomplete',
|
||||
'settings' => [
|
||||
'match_operator' => 'STARTS_WITH',
|
||||
],
|
||||
])
|
||||
->save();
|
||||
|
||||
$this->drupalGet('node/add/page');
|
||||
$page = $this->getSession()->getPage();
|
||||
|
||||
$autocomplete_field = $assert_session->waitForElement('css', '[name="' . $field_name . '[0][target_id]"].ui-autocomplete-input');
|
||||
$autocomplete_field->setValue('Test');
|
||||
$this->getSession()->getDriver()->keyDown($autocomplete_field->getXpath(), ' ');
|
||||
$assert_session->waitOnAutocomplete();
|
||||
|
||||
$results = $page->findAll('css', '.ui-autocomplete li');
|
||||
|
||||
$this->assertCount(1, $results);
|
||||
$assert_session->pageTextContains('Test page');
|
||||
$assert_session->pageTextNotContains('Page test');
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,370 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\FunctionalJavascriptTests;
|
||||
|
||||
use Behat\Mink\Element\NodeElement;
|
||||
use Behat\Mink\Exception\ElementHtmlException;
|
||||
use Behat\Mink\Exception\ElementNotFoundException;
|
||||
use Behat\Mink\Exception\UnsupportedDriverActionException;
|
||||
use Drupal\Tests\WebAssert;
|
||||
|
||||
/**
|
||||
* Defines a class with methods for asserting presence of elements during tests.
|
||||
*/
|
||||
class JSWebAssert extends WebAssert {
|
||||
|
||||
/**
|
||||
* Waits for AJAX request to be completed.
|
||||
*
|
||||
* @param int $timeout
|
||||
* (Optional) Timeout in milliseconds, defaults to 10000.
|
||||
* @param string $message
|
||||
* (optional) A message for exception.
|
||||
*
|
||||
* @throws \RuntimeException
|
||||
* When the request is not completed. If left blank, a default message will
|
||||
* be displayed.
|
||||
*/
|
||||
public function assertWaitOnAjaxRequest($timeout = 10000, $message = 'Unable to complete AJAX request.') {
|
||||
$condition = <<<JS
|
||||
(function() {
|
||||
function isAjaxing(instance) {
|
||||
return instance && instance.ajaxing === true;
|
||||
}
|
||||
return (
|
||||
// Assert no AJAX request is running (via jQuery or Drupal) and no
|
||||
// animation is running.
|
||||
(typeof jQuery === 'undefined' || (jQuery.active === 0 && jQuery(':animated').length === 0)) &&
|
||||
(typeof Drupal === 'undefined' || typeof Drupal.ajax === 'undefined' || !Drupal.ajax.instances.some(isAjaxing))
|
||||
);
|
||||
}());
|
||||
JS;
|
||||
$result = $this->session->wait($timeout, $condition);
|
||||
if (!$result) {
|
||||
throw new \RuntimeException($message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Waits for the specified selector and returns it when available.
|
||||
*
|
||||
* @param string $selector
|
||||
* The selector engine name. See ElementInterface::findAll() for the
|
||||
* supported selectors.
|
||||
* @param string|array $locator
|
||||
* The selector locator.
|
||||
* @param int $timeout
|
||||
* (Optional) Timeout in milliseconds, defaults to 10000.
|
||||
*
|
||||
* @return \Behat\Mink\Element\NodeElement|null
|
||||
* The page element node if found, NULL if not.
|
||||
*
|
||||
* @see \Behat\Mink\Element\ElementInterface::findAll()
|
||||
*/
|
||||
public function waitForElement($selector, $locator, $timeout = 10000) {
|
||||
$page = $this->session->getPage();
|
||||
|
||||
$result = $page->waitFor($timeout / 1000, function () use ($page, $selector, $locator) {
|
||||
return $page->find($selector, $locator);
|
||||
});
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Waits for the specified selector and returns it when available and visible.
|
||||
*
|
||||
* @param string $selector
|
||||
* The selector engine name. See ElementInterface::findAll() for the
|
||||
* supported selectors.
|
||||
* @param string|array $locator
|
||||
* The selector locator.
|
||||
* @param int $timeout
|
||||
* (Optional) Timeout in milliseconds, defaults to 10000.
|
||||
*
|
||||
* @return \Behat\Mink\Element\NodeElement|null
|
||||
* The page element node if found and visible, NULL if not.
|
||||
*
|
||||
* @see \Behat\Mink\Element\ElementInterface::findAll()
|
||||
*/
|
||||
public function waitForElementVisible($selector, $locator, $timeout = 10000) {
|
||||
$page = $this->session->getPage();
|
||||
|
||||
$result = $page->waitFor($timeout / 1000, function () use ($page, $selector, $locator) {
|
||||
$element = $page->find($selector, $locator);
|
||||
if (!empty($element) && $element->isVisible()) {
|
||||
return $element;
|
||||
}
|
||||
return NULL;
|
||||
});
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Waits for a button (input[type=submit|image|button|reset], button) with
|
||||
* specified locator and returns it.
|
||||
*
|
||||
* @param string $locator
|
||||
* The button ID, value or alt string.
|
||||
* @param int $timeout
|
||||
* (Optional) Timeout in milliseconds, defaults to 10000.
|
||||
*
|
||||
* @return \Behat\Mink\Element\NodeElement|null
|
||||
* The page element node if found, NULL if not.
|
||||
*/
|
||||
public function waitForButton($locator, $timeout = 10000) {
|
||||
return $this->waitForElement('named', ['button', $locator], $timeout);
|
||||
}
|
||||
|
||||
/**
|
||||
* Waits for a link with specified locator and returns it when available.
|
||||
*
|
||||
* @param string $locator
|
||||
* The link ID, title, text or image alt.
|
||||
* @param int $timeout
|
||||
* (Optional) Timeout in milliseconds, defaults to 10000.
|
||||
*
|
||||
* @return \Behat\Mink\Element\NodeElement|null
|
||||
* The page element node if found, NULL if not.
|
||||
*/
|
||||
public function waitForLink($locator, $timeout = 10000) {
|
||||
return $this->waitForElement('named', ['link', $locator], $timeout);
|
||||
}
|
||||
|
||||
/**
|
||||
* Waits for a field with specified locator and returns it when available.
|
||||
*
|
||||
* @param string $locator
|
||||
* The input ID, name or label for the field (input, textarea, select).
|
||||
* @param int $timeout
|
||||
* (Optional) Timeout in milliseconds, defaults to 10000.
|
||||
*
|
||||
* @return \Behat\Mink\Element\NodeElement|null
|
||||
* The page element node if found, NULL if not.
|
||||
*/
|
||||
public function waitForField($locator, $timeout = 10000) {
|
||||
return $this->waitForElement('named', ['field', $locator], $timeout);
|
||||
}
|
||||
|
||||
/**
|
||||
* Waits for an element by its id and returns it when available.
|
||||
*
|
||||
* @param string $id
|
||||
* The element ID.
|
||||
* @param int $timeout
|
||||
* (Optional) Timeout in milliseconds, defaults to 10000.
|
||||
*
|
||||
* @return \Behat\Mink\Element\NodeElement|null
|
||||
* The page element node if found, NULL if not.
|
||||
*/
|
||||
public function waitForId($id, $timeout = 10000) {
|
||||
return $this->waitForElement('named', ['id', $id], $timeout);
|
||||
}
|
||||
|
||||
/**
|
||||
* Waits for the jQuery autocomplete delay duration.
|
||||
*
|
||||
* @see https://api.jqueryui.com/autocomplete/#option-delay
|
||||
*/
|
||||
public function waitOnAutocomplete() {
|
||||
// Wait for the autocomplete to be visible.
|
||||
return $this->waitForElementVisible('css', '.ui-autocomplete li');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a node, or its specific corner, is visible in the viewport.
|
||||
*
|
||||
* Note: Always set the viewport size. This can be done with a PhantomJS
|
||||
* startup parameter or in your test with \Behat\Mink\Session->resizeWindow().
|
||||
* Drupal CI Javascript tests by default use a viewport of 1024x768px.
|
||||
*
|
||||
* @param string $selector_type
|
||||
* The element selector type (CSS, XPath).
|
||||
* @param string|array $selector
|
||||
* The element selector. Note: the first found element is used.
|
||||
* @param bool|string $corner
|
||||
* (Optional) The corner to test:
|
||||
* topLeft, topRight, bottomRight, bottomLeft.
|
||||
* Or FALSE to check the complete element (default).
|
||||
* @param string $message
|
||||
* (optional) A message for the exception.
|
||||
*
|
||||
* @throws \Behat\Mink\Exception\ElementHtmlException
|
||||
* When the element doesn't exist.
|
||||
* @throws \Behat\Mink\Exception\ElementNotFoundException
|
||||
* When the element is not visible in the viewport.
|
||||
*/
|
||||
public function assertVisibleInViewport($selector_type, $selector, $corner = FALSE, $message = 'Element is not visible in the viewport.') {
|
||||
$node = $this->session->getPage()->find($selector_type, $selector);
|
||||
if ($node === NULL) {
|
||||
if (is_array($selector)) {
|
||||
$selector = implode(' ', $selector);
|
||||
}
|
||||
throw new ElementNotFoundException($this->session->getDriver(), 'element', $selector_type, $selector);
|
||||
}
|
||||
|
||||
// Check if the node is visible on the page, which is a prerequisite of
|
||||
// being visible in the viewport.
|
||||
if (!$node->isVisible()) {
|
||||
throw new ElementHtmlException($message, $this->session->getDriver(), $node);
|
||||
}
|
||||
|
||||
$result = $this->checkNodeVisibilityInViewport($node, $corner);
|
||||
|
||||
if (!$result) {
|
||||
throw new ElementHtmlException($message, $this->session->getDriver(), $node);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a node, or its specific corner, is not visible in the viewport.
|
||||
*
|
||||
* Note: the node should exist in the page, otherwise this assertion fails.
|
||||
*
|
||||
* @param string $selector_type
|
||||
* The element selector type (CSS, XPath).
|
||||
* @param string|array $selector
|
||||
* The element selector. Note: the first found element is used.
|
||||
* @param bool|string $corner
|
||||
* (Optional) Corner to test: topLeft, topRight, bottomRight, bottomLeft.
|
||||
* Or FALSE to check the complete element (default).
|
||||
* @param string $message
|
||||
* (optional) A message for the exception.
|
||||
*
|
||||
* @throws \Behat\Mink\Exception\ElementHtmlException
|
||||
* When the element doesn't exist.
|
||||
* @throws \Behat\Mink\Exception\ElementNotFoundException
|
||||
* When the element is not visible in the viewport.
|
||||
*
|
||||
* @see \Drupal\FunctionalJavascriptTests\JSWebAssert::assertVisibleInViewport()
|
||||
*/
|
||||
public function assertNotVisibleInViewport($selector_type, $selector, $corner = FALSE, $message = 'Element is visible in the viewport.') {
|
||||
$node = $this->session->getPage()->find($selector_type, $selector);
|
||||
if ($node === NULL) {
|
||||
if (is_array($selector)) {
|
||||
$selector = implode(' ', $selector);
|
||||
}
|
||||
throw new ElementNotFoundException($this->session->getDriver(), 'element', $selector_type, $selector);
|
||||
}
|
||||
|
||||
$result = $this->checkNodeVisibilityInViewport($node, $corner);
|
||||
|
||||
if ($result) {
|
||||
throw new ElementHtmlException($message, $this->session->getDriver(), $node);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check the visibility of a node, or its specific corner.
|
||||
*
|
||||
* @param \Behat\Mink\Element\NodeElement $node
|
||||
* A valid node.
|
||||
* @param bool|string $corner
|
||||
* (Optional) Corner to test: topLeft, topRight, bottomRight, bottomLeft.
|
||||
* Or FALSE to check the complete element (default).
|
||||
*
|
||||
* @return bool
|
||||
* Returns TRUE if the node is visible in the viewport, FALSE otherwise.
|
||||
*
|
||||
* @throws \Behat\Mink\Exception\UnsupportedDriverActionException
|
||||
* When an invalid corner specification is given.
|
||||
*/
|
||||
private function checkNodeVisibilityInViewport(NodeElement $node, $corner = FALSE) {
|
||||
$xpath = $node->getXpath();
|
||||
|
||||
// Build the Javascript to test if the complete element or a specific corner
|
||||
// is in the viewport.
|
||||
switch ($corner) {
|
||||
case 'topLeft':
|
||||
$test_javascript_function = <<<JS
|
||||
function t(r, lx, ly) {
|
||||
return (
|
||||
r.top >= 0 &&
|
||||
r.top <= ly &&
|
||||
r.left >= 0 &&
|
||||
r.left <= lx
|
||||
)
|
||||
}
|
||||
JS;
|
||||
break;
|
||||
|
||||
case 'topRight':
|
||||
$test_javascript_function = <<<JS
|
||||
function t(r, lx, ly) {
|
||||
return (
|
||||
r.top >= 0 &&
|
||||
r.top <= ly &&
|
||||
r.right >= 0 &&
|
||||
r.right <= lx
|
||||
);
|
||||
}
|
||||
JS;
|
||||
break;
|
||||
|
||||
case 'bottomRight':
|
||||
$test_javascript_function = <<<JS
|
||||
function t(r, lx, ly) {
|
||||
return (
|
||||
r.bottom >= 0 &&
|
||||
r.bottom <= ly &&
|
||||
r.right >= 0 &&
|
||||
r.right <= lx
|
||||
);
|
||||
}
|
||||
JS;
|
||||
break;
|
||||
|
||||
case 'bottomLeft':
|
||||
$test_javascript_function = <<<JS
|
||||
function t(r, lx, ly) {
|
||||
return (
|
||||
r.bottom >= 0 &&
|
||||
r.bottom <= ly &&
|
||||
r.left >= 0 &&
|
||||
r.left <= lx
|
||||
);
|
||||
}
|
||||
JS;
|
||||
break;
|
||||
|
||||
case FALSE:
|
||||
$test_javascript_function = <<<JS
|
||||
function t(r, lx, ly) {
|
||||
return (
|
||||
r.top >= 0 &&
|
||||
r.left >= 0 &&
|
||||
r.bottom <= ly &&
|
||||
r.right <= lx
|
||||
);
|
||||
}
|
||||
JS;
|
||||
break;
|
||||
|
||||
// Throw an exception if an invalid corner parameter is given.
|
||||
default:
|
||||
throw new UnsupportedDriverActionException($corner, $this->session->getDriver());
|
||||
}
|
||||
|
||||
// Build the full Javascript test. The shared logic gets the corner
|
||||
// specific test logic injected.
|
||||
$full_javascript_visibility_test = <<<JS
|
||||
(function(t){
|
||||
var w = window,
|
||||
d = document,
|
||||
e = d.documentElement,
|
||||
n = d.evaluate("$xpath", d, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue,
|
||||
r = n.getBoundingClientRect(),
|
||||
lx = (w.innerWidth || e.clientWidth),
|
||||
ly = (w.innerHeight || e.clientHeight);
|
||||
|
||||
return t(r, lx, ly);
|
||||
}($test_javascript_function));
|
||||
JS;
|
||||
|
||||
// Check the visibility by injecting and executing the full Javascript test
|
||||
// script in the page.
|
||||
return $this->session->evaluateScript($full_javascript_visibility_test);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\FunctionalJavascriptTests;
|
||||
|
||||
/**
|
||||
* Tests Drupal settings retrieval in JavascriptTestBase tests.
|
||||
*
|
||||
* @group javascript
|
||||
*/
|
||||
class JavascriptGetDrupalSettingsTest extends WebDriverTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $modules = ['test_page_test'];
|
||||
|
||||
/**
|
||||
* Tests retrieval of Drupal settings.
|
||||
*
|
||||
* @see \Drupal\FunctionalJavascriptTests\WebDriverTestBase::getDrupalSettings()
|
||||
*/
|
||||
public function testGetDrupalSettings() {
|
||||
$this->drupalLogin($this->drupalCreateUser());
|
||||
$this->drupalGet('test-page');
|
||||
|
||||
// Check that we can read the JS settings.
|
||||
$js_settings = $this->getDrupalSettings();
|
||||
$this->assertSame('azAZ09();.,\\\/-_{}', $js_settings['test-setting']);
|
||||
|
||||
// Dynamically change the setting using Javascript.
|
||||
$script = <<<EndOfScript
|
||||
(function () {
|
||||
drupalSettings['test-setting'] = 'foo';
|
||||
})();
|
||||
EndOfScript;
|
||||
|
||||
$this->getSession()->evaluateScript($script);
|
||||
|
||||
// Check that the setting has been changed.
|
||||
$js_settings = $this->getDrupalSettings();
|
||||
$this->assertSame('foo', $js_settings['test-setting']);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\FunctionalJavascriptTests;
|
||||
|
||||
@trigger_error('The ' . __NAMESPACE__ . '\JavascriptTestBase is deprecated in Drupal 8.5.x and will be removed before Drupal 9.0.0. Instead, use ' . __NAMESPACE__ . '\WebDriverTestBase. See https://www.drupal.org/node/2945059', E_USER_DEPRECATED);
|
||||
|
||||
use Zumba\Mink\Driver\PhantomJSDriver;
|
||||
|
||||
/**
|
||||
* Runs a browser test using PhantomJS.
|
||||
*
|
||||
* Base class for testing browser interaction implemented in JavaScript.
|
||||
*
|
||||
* @deprecated in Drupal 8.6.x, will be removed before Drupal 9.0.0.
|
||||
* Use \Drupal\FunctionalJavascriptTests\WebDriverTestBase instead
|
||||
*
|
||||
* @see https://www.drupal.org/node/2945059
|
||||
*/
|
||||
abstract class JavascriptTestBase extends WebDriverTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected $minkDefaultDriverClass = PhantomJSDriver::class;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function assertSession($name = NULL) {
|
||||
// Return a WebAssert that supports status code and header assertions.
|
||||
return new JSWebAssert($this->getSession($name), $this->baseUrl);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\FunctionalJavascriptTests;
|
||||
|
||||
/**
|
||||
* Runs a browser test using PhantomJS.
|
||||
*
|
||||
* @deprecated in Drupal 8.6.0, will be removed before Drupal 9.0.0.
|
||||
* Use \Drupal\FunctionalJavascriptTests\WebDriverTestBase instead
|
||||
*
|
||||
* BC layer for testing browser interaction implemented in JavaScript.
|
||||
*/
|
||||
abstract class LegacyJavascriptTestBase extends JavascriptTestBase {
|
||||
|
||||
}
|
|
@ -0,0 +1,89 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\FunctionalJavascriptTests\Tests;
|
||||
|
||||
use Behat\Mink\Element\NodeElement;
|
||||
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
|
||||
|
||||
/**
|
||||
* Tests for the JSWebAssert class.
|
||||
*
|
||||
* @group javascript
|
||||
*/
|
||||
class JSWebAssertTest extends WebDriverTestBase {
|
||||
|
||||
/**
|
||||
* Required modules.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = ['js_webassert_test'];
|
||||
|
||||
/**
|
||||
* Tests that JSWebAssert assertions work correctly.
|
||||
*/
|
||||
public function testJsWebAssert() {
|
||||
$this->drupalGet('js_webassert_test_form');
|
||||
|
||||
$session = $this->getSession();
|
||||
$assert_session = $this->assertSession();
|
||||
$page = $session->getPage();
|
||||
|
||||
$test_button = $page->findButton('Add button');
|
||||
$test_link = $page->findButton('Add link');
|
||||
$test_field = $page->findButton('Add field');
|
||||
$test_id = $page->findButton('Add ID');
|
||||
$test_wait_on_ajax = $page->findButton('Test assertWaitOnAjaxRequest');
|
||||
$test_wait_on_element_visible = $page->findButton('Test waitForElementVisible');
|
||||
|
||||
// Test the wait...() methods by first checking the fields aren't available
|
||||
// and then are available after the wait method.
|
||||
$result = $page->findButton('Added button');
|
||||
$this->assertEmpty($result);
|
||||
$test_button->click();
|
||||
$result = $assert_session->waitForButton('Added button');
|
||||
$this->assertNotEmpty($result);
|
||||
$this->assertTrue($result instanceof NodeElement);
|
||||
|
||||
$result = $page->findLink('Added link');
|
||||
$this->assertEmpty($result);
|
||||
$test_link->click();
|
||||
$result = $assert_session->waitForLink('Added link');
|
||||
$this->assertNotEmpty($result);
|
||||
$this->assertTrue($result instanceof NodeElement);
|
||||
|
||||
$result = $page->findField('added_field');
|
||||
$this->assertEmpty($result);
|
||||
$test_field->click();
|
||||
$result = $assert_session->waitForField('added_field');
|
||||
$this->assertNotEmpty($result);
|
||||
$this->assertTrue($result instanceof NodeElement);
|
||||
|
||||
$result = $page->findById('js_webassert_test_field_id');
|
||||
$this->assertEmpty($result);
|
||||
$test_id->click();
|
||||
$result = $assert_session->waitForId('js_webassert_test_field_id');
|
||||
$this->assertNotEmpty($result);
|
||||
$this->assertTrue($result instanceof NodeElement);
|
||||
|
||||
// Test waitOnAjaxRequest. Verify the element is available after the wait
|
||||
// and the behaviors have run on completing by checking the value.
|
||||
$result = $page->findField('test_assert_wait_on_ajax_input');
|
||||
$this->assertEmpty($result);
|
||||
$test_wait_on_ajax->click();
|
||||
$assert_session->assertWaitOnAjaxRequest();
|
||||
$result = $page->findField('test_assert_wait_on_ajax_input');
|
||||
$this->assertNotEmpty($result);
|
||||
$this->assertTrue($result instanceof NodeElement);
|
||||
$this->assertEquals('js_webassert_test', $result->getValue());
|
||||
|
||||
$result = $page->findButton('Added WaitForElementVisible');
|
||||
$this->assertEmpty($result);
|
||||
$test_wait_on_element_visible->click();
|
||||
$result = $assert_session->waitForElementVisible('named', ['button', 'Added WaitForElementVisible']);
|
||||
$this->assertNotEmpty($result);
|
||||
$this->assertTrue($result instanceof NodeElement);
|
||||
$this->assertEquals(TRUE, $result->isVisible());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\FunctionalJavascriptTests\Tests;
|
||||
|
||||
use Drupal\FunctionalJavascriptTests\DrupalSelenium2Driver;
|
||||
|
||||
/**
|
||||
* Tests for the JSWebAssert class using webdriver.
|
||||
*
|
||||
* @group javascript
|
||||
*/
|
||||
class JSWebWithWebDriverAssertTest extends JSWebAssertTest {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected $minkDefaultDriverClass = DrupalSelenium2Driver::class;
|
||||
|
||||
}
|
|
@ -0,0 +1,110 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\FunctionalJavascriptTests;
|
||||
|
||||
use WebDriver\Service\CurlService;
|
||||
use WebDriver\Exception\CurlExec;
|
||||
use WebDriver\Exception as WebDriverException;
|
||||
|
||||
/**
|
||||
* Provides a curl service to interact with Selenium driver.
|
||||
*
|
||||
* Extends WebDriver\Service\CurlService to solve problem with race conditions,
|
||||
* when multiple processes requests.
|
||||
*/
|
||||
class WebDriverCurlService extends CurlService {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function execute($requestMethod, $url, $parameters = NULL, $extraOptions = []) {
|
||||
$extraOptions += [
|
||||
CURLOPT_FAILONERROR => TRUE,
|
||||
];
|
||||
$retries = 0;
|
||||
while ($retries < 10) {
|
||||
try {
|
||||
$customHeaders = [
|
||||
'Content-Type: application/json;charset=UTF-8',
|
||||
'Accept: application/json;charset=UTF-8',
|
||||
];
|
||||
|
||||
$curl = curl_init($url);
|
||||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
|
||||
|
||||
switch ($requestMethod) {
|
||||
case 'GET':
|
||||
break;
|
||||
|
||||
case 'POST':
|
||||
if ($parameters && is_array($parameters)) {
|
||||
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($parameters));
|
||||
}
|
||||
else {
|
||||
$customHeaders[] = 'Content-Length: 0';
|
||||
}
|
||||
|
||||
// Suppress "Expect: 100-continue" header automatically added by
|
||||
// cURL that causes a 1 second delay if the remote server does not
|
||||
// support Expect.
|
||||
$customHeaders[] = 'Expect:';
|
||||
|
||||
curl_setopt($curl, CURLOPT_POST, TRUE);
|
||||
break;
|
||||
|
||||
case 'DELETE':
|
||||
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
|
||||
break;
|
||||
|
||||
case 'PUT':
|
||||
if ($parameters && is_array($parameters)) {
|
||||
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($parameters));
|
||||
}
|
||||
else {
|
||||
$customHeaders[] = 'Content-Length: 0';
|
||||
}
|
||||
|
||||
// Suppress "Expect: 100-continue" header automatically added by
|
||||
// cURL that causes a 1 second delay if the remote server does not
|
||||
// support Expect.
|
||||
$customHeaders[] = 'Expect:';
|
||||
|
||||
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
|
||||
break;
|
||||
}
|
||||
|
||||
foreach ($extraOptions as $option => $value) {
|
||||
curl_setopt($curl, $option, $value);
|
||||
}
|
||||
|
||||
curl_setopt($curl, CURLOPT_HTTPHEADER, $customHeaders);
|
||||
|
||||
$rawResult = trim(curl_exec($curl));
|
||||
|
||||
$info = curl_getinfo($curl);
|
||||
$info['request_method'] = $requestMethod;
|
||||
|
||||
if (array_key_exists(CURLOPT_FAILONERROR, $extraOptions) && $extraOptions[CURLOPT_FAILONERROR] && CURLE_GOT_NOTHING !== ($errno = curl_errno($curl)) && $error = curl_error($curl)) {
|
||||
curl_close($curl);
|
||||
|
||||
throw WebDriverException::factory(WebDriverException::CURL_EXEC, sprintf("Curl error thrown for http %s to %s%s\n\n%s", $requestMethod, $url, $parameters && is_array($parameters) ? ' with params: ' . json_encode($parameters) : '', $error));
|
||||
}
|
||||
|
||||
curl_close($curl);
|
||||
|
||||
$result = json_decode($rawResult, TRUE);
|
||||
if (isset($result['status']) && $result['status'] === WebDriverException::STALE_ELEMENT_REFERENCE) {
|
||||
usleep(100000);
|
||||
$retries++;
|
||||
continue;
|
||||
}
|
||||
return [$rawResult, $info];
|
||||
}
|
||||
catch (CurlExec $exception) {
|
||||
$retries++;
|
||||
}
|
||||
}
|
||||
throw WebDriverException::factory(WebDriverException::CURL_EXEC, sprintf("Curl error thrown for http %s to %s%s\n\n%s", $requestMethod, $url, $parameters && is_array($parameters) ? ' with params: ' . json_encode($parameters) : '', $error));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,211 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\FunctionalJavascriptTests;
|
||||
|
||||
use Behat\Mink\Exception\DriverException;
|
||||
use Drupal\Tests\BrowserTestBase;
|
||||
use Zumba\GastonJS\Exception\DeadClient;
|
||||
use Zumba\Mink\Driver\PhantomJSDriver;
|
||||
|
||||
/**
|
||||
* Runs a browser test using a driver that supports Javascript.
|
||||
*
|
||||
* Base class for testing browser interaction implemented in JavaScript.
|
||||
*/
|
||||
abstract class WebDriverTestBase extends BrowserTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* To use a legacy phantomjs based approach, please use PhantomJSDriver::class.
|
||||
*/
|
||||
protected $minkDefaultDriverClass = DrupalSelenium2Driver::class;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function initMink() {
|
||||
if ($this->minkDefaultDriverClass === DrupalSelenium2Driver::class) {
|
||||
$this->minkDefaultDriverArgs = ['chrome', NULL, 'http://localhost:4444'];
|
||||
}
|
||||
elseif ($this->minkDefaultDriverClass === PhantomJSDriver::class) {
|
||||
// Set up the template cache used by the PhantomJS mink driver.
|
||||
$path = $this->tempFilesDirectory . DIRECTORY_SEPARATOR . 'browsertestbase-templatecache';
|
||||
$this->minkDefaultDriverArgs = [
|
||||
'http://127.0.0.1:8510',
|
||||
$path,
|
||||
];
|
||||
if (!file_exists($path)) {
|
||||
mkdir($path);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
return parent::initMink();
|
||||
}
|
||||
catch (DeadClient $e) {
|
||||
$this->markTestSkipped('PhantomJS is either not installed or not running. Start it via phantomjs --ssl-protocol=any --ignore-ssl-errors=true vendor/jcalderonzumba/gastonjs/src/Client/main.js 8510 1024 768&');
|
||||
}
|
||||
catch (DriverException $e) {
|
||||
if ($this->minkDefaultDriverClass === DrupalSelenium2Driver::class) {
|
||||
$this->markTestSkipped("The test wasn't able to connect to your webdriver instance. For more information read core/tests/README.md.\n\nThe original message while starting Mink: {$e->getMessage()}");
|
||||
}
|
||||
else {
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
catch (\Exception $e) {
|
||||
$this->markTestSkipped('An unexpected error occurred while starting Mink: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function tearDown() {
|
||||
if ($this->mink) {
|
||||
// Wait for all requests to finish. It is possible that an AJAX request is
|
||||
// still on-going.
|
||||
$result = $this->getSession()->wait(5000, '(typeof(jQuery)=="undefined" || (0 === jQuery.active && 0 === jQuery(\':animated\').length))');
|
||||
if (!$result) {
|
||||
// If the wait is unsuccessful, there may still be an AJAX request in
|
||||
// progress. If we tear down now, then this AJAX request may fail with
|
||||
// missing database tables, because tear down will have removed them.
|
||||
// Rather than allow it to fail, throw an explicit exception now
|
||||
// explaining what the problem is.
|
||||
throw new \RuntimeException('Unfinished AJAX requests while tearing down a test');
|
||||
}
|
||||
}
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getMinkDriverArgs() {
|
||||
if ($this->minkDefaultDriverClass === DrupalSelenium2Driver::class) {
|
||||
return getenv('MINK_DRIVER_ARGS_WEBDRIVER') ?: getenv('MINK_DRIVER_ARGS_PHANTOMJS') ?: parent::getMinkDriverArgs();
|
||||
}
|
||||
elseif ($this->minkDefaultDriverClass === PhantomJSDriver::class) {
|
||||
return getenv('MINK_DRIVER_ARGS_PHANTOMJS') ?: parent::getMinkDriverArgs();
|
||||
}
|
||||
return parent::getMinkDriverArgs();
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the element with the given CSS selector is visible.
|
||||
*
|
||||
* @param string $css_selector
|
||||
* The CSS selector identifying the element to check.
|
||||
* @param string $message
|
||||
* Optional message to show alongside the assertion.
|
||||
*
|
||||
* @deprecated in Drupal 8.1.0, will be removed before Drupal 9.0.0. Use
|
||||
* \Behat\Mink\Element\NodeElement::isVisible() instead.
|
||||
*/
|
||||
protected function assertElementVisible($css_selector, $message = '') {
|
||||
$this->assertTrue($this->getSession()->getDriver()->isVisible($this->cssSelectToXpath($css_selector)), $message);
|
||||
@trigger_error('The ' . __METHOD__ . ' method is deprecated since version 8.1.0 and will be removed in 9.0.0. Use \Behat\Mink\Element\NodeElement::isVisible() instead.', E_USER_DEPRECATED);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the element with the given CSS selector is not visible.
|
||||
*
|
||||
* @param string $css_selector
|
||||
* The CSS selector identifying the element to check.
|
||||
* @param string $message
|
||||
* Optional message to show alongside the assertion.
|
||||
*
|
||||
* @deprecated in Drupal 8.1.0, will be removed before Drupal 9.0.0. Use
|
||||
* \Behat\Mink\Element\NodeElement::isVisible() instead.
|
||||
*/
|
||||
protected function assertElementNotVisible($css_selector, $message = '') {
|
||||
$this->assertFalse($this->getSession()->getDriver()->isVisible($this->cssSelectToXpath($css_selector)), $message);
|
||||
@trigger_error('The ' . __METHOD__ . ' method is deprecated since version 8.1.0 and will be removed in 9.0.0. Use \Behat\Mink\Element\NodeElement::isVisible() instead.', E_USER_DEPRECATED);
|
||||
}
|
||||
|
||||
/**
|
||||
* Waits for the given time or until the given JS condition becomes TRUE.
|
||||
*
|
||||
* @param string $condition
|
||||
* JS condition to wait until it becomes TRUE.
|
||||
* @param int $timeout
|
||||
* (Optional) Timeout in milliseconds, defaults to 10000.
|
||||
* @param string $message
|
||||
* (optional) A message to display with the assertion. If left blank, a
|
||||
* default message will be displayed.
|
||||
*
|
||||
* @throws \PHPUnit_Framework_AssertionFailedError
|
||||
*
|
||||
* @see \Behat\Mink\Driver\DriverInterface::evaluateScript()
|
||||
*/
|
||||
protected function assertJsCondition($condition, $timeout = 10000, $message = '') {
|
||||
$message = $message ?: "Javascript condition met:\n" . $condition;
|
||||
$result = $this->getSession()->getDriver()->wait($timeout, $condition);
|
||||
$this->assertTrue($result, $message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a screenshot.
|
||||
*
|
||||
* @param string $filename
|
||||
* The file name of the resulting screenshot. If using the default phantomjs
|
||||
* driver then this should be a JPG filename.
|
||||
* @param bool $set_background_color
|
||||
* (optional) By default this method will set the background color to white.
|
||||
* Set to FALSE to override this behaviour.
|
||||
*
|
||||
* @throws \Behat\Mink\Exception\UnsupportedDriverActionException
|
||||
* When operation not supported by the driver.
|
||||
* @throws \Behat\Mink\Exception\DriverException
|
||||
* When the operation cannot be done.
|
||||
*/
|
||||
protected function createScreenshot($filename, $set_background_color = TRUE) {
|
||||
$session = $this->getSession();
|
||||
if ($set_background_color) {
|
||||
$session->executeScript("document.body.style.backgroundColor = 'white';");
|
||||
}
|
||||
$image = $session->getScreenshot();
|
||||
file_put_contents($filename, $image);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function assertSession($name = NULL) {
|
||||
return new WebDriverWebAssert($this->getSession($name), $this->baseUrl);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current Drupal javascript settings and parses into an array.
|
||||
*
|
||||
* Unlike BrowserTestBase::getDrupalSettings(), this implementation reads the
|
||||
* current values of drupalSettings, capturing all changes made via javascript
|
||||
* after the page was loaded.
|
||||
*
|
||||
* @return array
|
||||
* The Drupal javascript settings array.
|
||||
*
|
||||
* @see \Drupal\Tests\BrowserTestBase::getDrupalSettings()
|
||||
*/
|
||||
protected function getDrupalSettings() {
|
||||
$script = <<<EndOfScript
|
||||
(function () {
|
||||
if (typeof drupalSettings !== 'undefined') {
|
||||
return drupalSettings;
|
||||
}
|
||||
})();
|
||||
EndOfScript;
|
||||
|
||||
return $this->getSession()->evaluateScript($script) ?: [];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getHtmlOutputHeaders() {
|
||||
// The webdriver API does not support fetching headers.
|
||||
return '';
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,110 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\FunctionalJavascriptTests;
|
||||
|
||||
/**
|
||||
* Defines a JSWebAssert with no support for status code and header assertions.
|
||||
*/
|
||||
class WebDriverWebAssert extends JSWebAssert {
|
||||
|
||||
/**
|
||||
* The use of statusCodeEquals() is not available.
|
||||
*
|
||||
* @param int $code
|
||||
* The status code.
|
||||
*/
|
||||
public function statusCodeEquals($code) {
|
||||
@trigger_error('Support for statusCodeEquals is to be dropped from Javascript tests. See https://www.drupal.org/node/2857562.');
|
||||
parent::statusCodeEquals($code);
|
||||
}
|
||||
|
||||
/**
|
||||
* The use of statusCodeNotEquals() is not available.
|
||||
*
|
||||
* @param int $code
|
||||
* The status code.
|
||||
*/
|
||||
public function statusCodeNotEquals($code) {
|
||||
@trigger_error('Support for statusCodeNotEquals is to be dropped from Javascript tests. See https://www.drupal.org/node/2857562.');
|
||||
parent::statusCodeNotEquals($code);
|
||||
}
|
||||
|
||||
/**
|
||||
* The use of responseHeaderEquals() is not available.
|
||||
*
|
||||
* @param string $name
|
||||
* The name of the header.
|
||||
* @param string $value
|
||||
* The value to check the header against.
|
||||
*/
|
||||
public function responseHeaderEquals($name, $value) {
|
||||
@trigger_error('Support for responseHeaderEquals is to be dropped from Javascript tests. See https://www.drupal.org/node/2857562.');
|
||||
parent::responseHeaderEquals($name, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* The use of responseHeaderNotEquals() is not available.
|
||||
*
|
||||
* @param string $name
|
||||
* The name of the header.
|
||||
* @param string $value
|
||||
* The value to check the header against.
|
||||
*/
|
||||
public function responseHeaderNotEquals($name, $value) {
|
||||
@trigger_error('Support for responseHeaderNotEquals is to be dropped from Javascript tests. See https://www.drupal.org/node/2857562.');
|
||||
parent::responseHeaderNotEquals($name, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* The use of responseHeaderContains() is not available.
|
||||
*
|
||||
* @param string $name
|
||||
* The name of the header.
|
||||
* @param string $value
|
||||
* The value to check the header against.
|
||||
*/
|
||||
public function responseHeaderContains($name, $value) {
|
||||
@trigger_error('Support for responseHeaderContains is to be dropped from Javascript tests. See https://www.drupal.org/node/2857562.');
|
||||
parent::responseHeaderContains($name, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* The use of responseHeaderNotContains() is not available.
|
||||
*
|
||||
* @param string $name
|
||||
* The name of the header.
|
||||
* @param string $value
|
||||
* The value to check the header against.
|
||||
*/
|
||||
public function responseHeaderNotContains($name, $value) {
|
||||
@trigger_error('Support for responseHeaderNotContains is to be dropped from Javascript tests. See https://www.drupal.org/node/2857562.');
|
||||
parent::responseHeaderNotContains($name, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* The use of responseHeaderMatches() is not available.
|
||||
*
|
||||
* @param string $name
|
||||
* The name of the header.
|
||||
* @param string $regex
|
||||
* The value to check the header against.
|
||||
*/
|
||||
public function responseHeaderMatches($name, $regex) {
|
||||
@trigger_error('Support for responseHeaderMatches is to be dropped from Javascript tests. See https://www.drupal.org/node/2857562.');
|
||||
parent::responseHeaderMatches($name, $regex);
|
||||
}
|
||||
|
||||
/**
|
||||
* The use of responseHeaderNotMatches() is not available.
|
||||
*
|
||||
* @param string $name
|
||||
* The name of the header.
|
||||
* @param string $regex
|
||||
* The value to check the header against.
|
||||
*/
|
||||
public function responseHeaderNotMatches($name, $regex) {
|
||||
@trigger_error('Support for responseHeaderNotMatches is to be dropped from Javascript tests. See https://www.drupal.org/node/2857562.');
|
||||
parent::responseHeaderNotMatches($name, $regex);
|
||||
}
|
||||
|
||||
}
|
Reference in a new issue