コード例 #1
0
ファイル: CommandsTest.php プロジェクト: papillon-cendre/d8
 /**
  * Tests the various Ajax Commands.
  */
 function testAjaxCommands()
 {
     $form_path = 'ajax_forms_test_ajax_commands_form';
     $web_user = $this->drupalCreateUser(array('access content'));
     $this->drupalLogin($web_user);
     $edit = array();
     // Tests the 'add_css' command.
     $commands = $this->drupalPostAjaxForm($form_path, $edit, array('op' => t("AJAX 'add_css' command")));
     $expected = new AddCssCommand('my/file.css');
     $this->assertCommand($commands, $expected->render(), "'add_css' AJAX command issued with correct data.");
     // Tests the 'after' command.
     $commands = $this->drupalPostAjaxForm($form_path, $edit, array('op' => t("AJAX 'After': Click to put something after the div")));
     $expected = new AfterCommand('#after_div', 'This will be placed after');
     $this->assertCommand($commands, $expected->render(), "'after' AJAX command issued with correct data.");
     // Tests the 'alert' command.
     $commands = $this->drupalPostAjaxForm($form_path, $edit, array('op' => t("AJAX 'Alert': Click to alert")));
     $expected = new AlertCommand(t('Alert'));
     $this->assertCommand($commands, $expected->render(), "'alert' AJAX Command issued with correct text.");
     // Tests the 'append' command.
     $commands = $this->drupalPostAjaxForm($form_path, $edit, array('op' => t("AJAX 'Append': Click to append something")));
     $expected = new AppendCommand('#append_div', 'Appended text');
     $this->assertCommand($commands, $expected->render(), "'append' AJAX command issued with correct data.");
     // Tests the 'before' command.
     $commands = $this->drupalPostAjaxForm($form_path, $edit, array('op' => t("AJAX 'before': Click to put something before the div")));
     $expected = new BeforeCommand('#before_div', 'Before text');
     $this->assertCommand($commands, $expected->render(), "'before' AJAX command issued with correct data.");
     // Tests the 'changed' command.
     $commands = $this->drupalPostAjaxForm($form_path, $edit, array('op' => t("AJAX changed: Click to mark div changed.")));
     $expected = new ChangedCommand('#changed_div');
     $this->assertCommand($commands, $expected->render(), "'changed' AJAX command issued with correct selector.");
     // Tests the 'changed' command using the second argument.
     $commands = $this->drupalPostAjaxForm($form_path, $edit, array('op' => t("AJAX changed: Click to mark div changed with asterisk.")));
     $expected = new ChangedCommand('#changed_div', '#changed_div_mark_this');
     $this->assertCommand($commands, $expected->render(), "'changed' AJAX command (with asterisk) issued with correct selector.");
     // Tests the 'css' command.
     $commands = $this->drupalPostAjaxForm($form_path, $edit, array('op' => t("Set the '#box' div to be blue.")));
     $expected = new CssCommand('#css_div', array('background-color' => 'blue'));
     $this->assertCommand($commands, $expected->render(), "'css' AJAX command issued with correct selector.");
     // Tests the 'data' command.
     $commands = $this->drupalPostAjaxForm($form_path, $edit, array('op' => t("AJAX data command: Issue command.")));
     $expected = new DataCommand('#data_div', 'testkey', 'testvalue');
     $this->assertCommand($commands, $expected->render(), "'data' AJAX command issued with correct key and value.");
     // Tests the 'html' command.
     $commands = $this->drupalPostAjaxForm($form_path, $edit, array('op' => t("AJAX html: Replace the HTML in a selector.")));
     $expected = new HtmlCommand('#html_div', 'replacement text');
     $this->assertCommand($commands, $expected->render(), "'html' AJAX command issued with correct data.");
     // Tests the 'insert' command.
     $commands = $this->drupalPostAjaxForm($form_path, $edit, array('op' => t("AJAX insert: Let client insert based on #ajax['method'].")));
     $expected = new InsertCommand('#insert_div', 'insert replacement text');
     $this->assertCommand($commands, $expected->render(), "'insert' AJAX command issued with correct data.");
     // Tests the 'invoke' command.
     $commands = $this->drupalPostAjaxForm($form_path, $edit, array('op' => t("AJAX invoke command: Invoke addClass() method.")));
     $expected = new InvokeCommand('#invoke_div', 'addClass', array('error'));
     $this->assertCommand($commands, $expected->render(), "'invoke' AJAX command issued with correct method and argument.");
     // Tests the 'prepend' command.
     $commands = $this->drupalPostAjaxForm($form_path, $edit, array('op' => t("AJAX 'prepend': Click to prepend something")));
     $expected = new PrependCommand('#prepend_div', 'prepended text');
     $this->assertCommand($commands, $expected->render(), "'prepend' AJAX command issued with correct data.");
     // Tests the 'remove' command.
     $commands = $this->drupalPostAjaxForm($form_path, $edit, array('op' => t("AJAX 'remove': Click to remove text")));
     $expected = new RemoveCommand('#remove_text');
     $this->assertCommand($commands, $expected->render(), "'remove' AJAX command issued with correct command and selector.");
     // Tests the 'restripe' command.
     $commands = $this->drupalPostAjaxForm($form_path, $edit, array('op' => t("AJAX 'restripe' command")));
     $expected = new RestripeCommand('#restripe_table');
     $this->assertCommand($commands, $expected->render(), "'restripe' AJAX command issued with correct selector.");
     // Tests the 'settings' command.
     $commands = $this->drupalPostAjaxForm($form_path, $edit, array('op' => t("AJAX 'settings' command")));
     $expected = new SettingsCommand(array('ajax_forms_test' => array('foo' => 42)));
     $this->assertCommand($commands, $expected->render(), "'settings' AJAX command issued with correct data.");
 }
コード例 #2
0
ファイル: FrameworkTest.php プロジェクト: nstielau/drops-8
 /**
  * Tests that new JavaScript and CSS files are lazy-loaded on an AJAX request.
  */
 public function testLazyLoad()
 {
     $asset_resolver = \Drupal::service('asset.resolver');
     $css_collection_renderer = \Drupal::service('asset.css.collection_renderer');
     $js_collection_renderer = \Drupal::service('asset.js.collection_renderer');
     $renderer = \Drupal::service('renderer');
     $expected = array('setting_name' => 'ajax_forms_test_lazy_load_form_submit', 'setting_value' => 'executed', 'library_1' => 'system/admin', 'library_2' => 'system/drupal.system');
     // Get the base page.
     $this->drupalGet('ajax_forms_test_lazy_load_form');
     $original_settings = $this->getDrupalSettings();
     $original_libraries = explode(',', $original_settings['ajaxPageState']['libraries']);
     // Verify that the base page doesn't have the settings and files that are to
     // be lazy loaded as part of the next requests.
     $this->assertTrue(!isset($original_settings[$expected['setting_name']]), format_string('Page originally lacks the %setting, as expected.', array('%setting' => $expected['setting_name'])));
     $this->assertTrue(!in_array($expected['library_1'], $original_libraries), format_string('Page originally lacks the %library library, as expected.', array('%library' => $expected['library_1'])));
     $this->assertTrue(!in_array($expected['library_2'], $original_libraries), format_string('Page originally lacks the %library library, as expected.', array('%library' => $expected['library_2'])));
     // Calculate the expected CSS and JS.
     $assets = new AttachedAssets();
     $assets->setLibraries([$expected['library_1']])->setAlreadyLoadedLibraries($original_libraries);
     $css_render_array = $css_collection_renderer->render($asset_resolver->getCssAssets($assets, FALSE));
     $expected_css_html = $renderer->render($css_render_array);
     $assets->setLibraries([$expected['library_2']])->setAlreadyLoadedLibraries($original_libraries);
     $js_assets = $asset_resolver->getJsAssets($assets, FALSE)[1];
     unset($js_assets['drupalSettings']);
     $js_render_array = $js_collection_renderer->render($js_assets);
     $expected_js_html = $renderer->render($js_render_array);
     // Submit the AJAX request without triggering files getting added.
     $commands = $this->drupalPostAjaxForm(NULL, array('add_files' => FALSE), array('op' => t('Submit')));
     $new_settings = $this->getDrupalSettings();
     $new_libraries = explode(',', $new_settings['ajaxPageState']['libraries']);
     // Verify the setting was not added when not expected.
     $this->assertTrue(!isset($new_settings[$expected['setting_name']]), format_string('Page still lacks the %setting, as expected.', array('%setting' => $expected['setting_name'])));
     $this->assertTrue(!in_array($expected['library_1'], $new_libraries), format_string('Page still lacks the %library library, as expected.', array('%library' => $expected['library_1'])));
     $this->assertTrue(!in_array($expected['library_2'], $new_libraries), format_string('Page still lacks the %library library, as expected.', array('%library' => $expected['library_2'])));
     // Verify a settings command does not add CSS or scripts to drupalSettings
     // and no command inserts the corresponding tags on the page.
     $found_settings_command = FALSE;
     $found_markup_command = FALSE;
     foreach ($commands as $command) {
         if ($command['command'] == 'settings' && (array_key_exists('css', $command['settings']['ajaxPageState']) || array_key_exists('js', $command['settings']['ajaxPageState']))) {
             $found_settings_command = TRUE;
         }
         if (isset($command['data']) && ($command['data'] == $expected_js_html || $command['data'] == $expected_css_html)) {
             $found_markup_command = TRUE;
         }
     }
     $this->assertFalse($found_settings_command, format_string('Page state still lacks the %library_1 and %library_2 libraries, as expected.', array('%library_1' => $expected['library_1'], '%library_2' => $expected['library_2'])));
     $this->assertFalse($found_markup_command, format_string('Page still lacks the %library_1 and %library_2 libraries, as expected.', array('%library_1' => $expected['library_1'], '%library_2' => $expected['library_2'])));
     // Submit the AJAX request and trigger adding files.
     $commands = $this->drupalPostAjaxForm(NULL, array('add_files' => TRUE), array('op' => t('Submit')));
     $new_settings = $this->getDrupalSettings();
     $new_libraries = explode(',', $new_settings['ajaxPageState']['libraries']);
     // Verify the expected setting was added, both to drupalSettings, and as
     // the first AJAX command.
     $this->assertIdentical($new_settings[$expected['setting_name']], $expected['setting_value'], format_string('Page now has the %setting.', array('%setting' => $expected['setting_name'])));
     $expected_command = new SettingsCommand(array($expected['setting_name'] => $expected['setting_value']), TRUE);
     $this->assertCommand(array_slice($commands, 0, 1), $expected_command->render(), format_string('The settings command was first.'));
     // Verify the expected CSS file was added, both to drupalSettings, and as
     // the second AJAX command for inclusion into the HTML.
     $this->assertTrue(in_array($expected['library_1'], $new_libraries), format_string('Page state now has the %library library.', array('%library' => $expected['library_1'])));
     $this->assertCommand(array_slice($commands, 1, 1), array('data' => $expected_css_html), format_string('Page now has the %library library.', array('%library' => $expected['library_1'])));
     // Verify the expected JS file was added, both to drupalSettings, and as
     // the third AJAX command for inclusion into the HTML. By testing for an
     // exact HTML string containing the SCRIPT tag, we also ensure that
     // unexpected JavaScript code, such as a jQuery.extend() that would
     // potentially clobber rather than properly merge settings, didn't
     // accidentally get added.
     $this->assertTrue(in_array($expected['library_2'], $new_libraries), format_string('Page state now has the %library library.', array('%library' => $expected['library_2'])));
     $this->assertCommand(array_slice($commands, 2, 1), array('data' => $expected_js_html), format_string('Page now has the %library library.', array('%library' => $expected['library_2'])));
 }
コード例 #3
0
ファイル: AjaxCommandsTest.php プロジェクト: ddrozdik/dmaps
 /**
  * @covers \Drupal\Core\Ajax\SettingsCommand
  */
 public function testSettingsCommand()
 {
     $command = new SettingsCommand(array('key' => 'value'), TRUE);
     $expected = array('command' => 'settings', 'settings' => array('key' => 'value'), 'merge' => TRUE);
     $this->assertEquals($expected, $command->render());
 }
コード例 #4
0
ファイル: FrameworkTest.php プロジェクト: anyforsoft/csua_d8
 /**
  * Tests that new JavaScript and CSS files are lazy-loaded on an AJAX request.
  */
 public function testLazyLoad()
 {
     $expected = array('setting_name' => 'ajax_forms_test_lazy_load_form_submit', 'setting_value' => 'executed', 'css' => drupal_get_path('module', 'system') . '/css/system.admin.css', 'js' => drupal_get_path('module', 'system') . '/system.js');
     // CSS files are stored by basename, see _drupal_add_css().
     $expected_css_basename = drupal_basename($expected['css']);
     // @todo D8: Add a drupal_css_defaults() helper function.
     $expected_css_html = drupal_get_css(array($expected_css_basename => array('type' => 'file', 'group' => CSS_AGGREGATE_DEFAULT, 'weight' => 0, 'every_page' => FALSE, 'media' => 'all', 'preprocess' => TRUE, 'data' => $expected['css'], 'browsers' => array('IE' => TRUE, '!IE' => TRUE))), TRUE);
     $expected_js_html = drupal_get_js('header', array($expected['js'] => drupal_js_defaults($expected['js'])), TRUE);
     // Get the base page.
     $this->drupalGet('ajax_forms_test_lazy_load_form');
     $original_settings = $this->getDrupalSettings();
     $original_css = $original_settings['ajaxPageState']['css'];
     $original_js = $original_settings['ajaxPageState']['js'];
     // Verify that the base page doesn't have the settings and files that are to
     // be lazy loaded as part of the next requests.
     $this->assertTrue(!isset($original_settings[$expected['setting_name']]), format_string('Page originally lacks the %setting, as expected.', array('%setting' => $expected['setting_name'])));
     $this->assertTrue(!isset($original_css[$expected['css']]), format_string('Page originally lacks the %css file, as expected.', array('%css' => $expected['css'])));
     $this->assertTrue(!isset($original_js[$expected['js']]), format_string('Page originally lacks the %js file, as expected.', array('%js' => $expected['js'])));
     // Submit the AJAX request without triggering files getting added.
     $commands = $this->drupalPostAjaxForm(NULL, array('add_files' => FALSE), array('op' => t('Submit')));
     $new_settings = $this->getDrupalSettings();
     $new_css = $new_settings['ajaxPageState']['css'];
     $new_js = $new_settings['ajaxPageState']['js'];
     // Verify the setting was not added when not expected.
     $this->assertTrue(!isset($new_settings[$expected['setting_name']]), format_string('Page still lacks the %setting, as expected.', array('%setting' => $expected['setting_name'])));
     $this->assertTrue(!isset($new_css[$expected['css']]), format_string('Page still lacks the %css file, as expected.', array('%css' => $expected['css'])));
     $this->assertTrue(!isset($new_js[$expected['js']]), format_string('Page still lacks the %js file, as expected.', array('%js' => $expected['js'])));
     // Verify a settings command does not add CSS or scripts to drupalSettings
     // and no command inserts the corresponding tags on the page.
     $found_settings_command = FALSE;
     $found_markup_command = FALSE;
     foreach ($commands as $command) {
         if ($command['command'] == 'settings' && (array_key_exists('css', $command['settings']['ajaxPageState']) || array_key_exists('js', $command['settings']['ajaxPageState']))) {
             $found_settings_command = TRUE;
         }
         if (isset($command['data']) && ($command['data'] == $expected_js_html || $command['data'] == $expected_css_html)) {
             $found_markup_command = TRUE;
         }
     }
     $this->assertFalse($found_settings_command, format_string('Page state still lacks the %css and %js files, as expected.', array('%css' => $expected['css'], '%js' => $expected['js'])));
     $this->assertFalse($found_markup_command, format_string('Page still lacks the %css and %js files, as expected.', array('%css' => $expected['css'], '%js' => $expected['js'])));
     // Submit the AJAX request and trigger adding files.
     $commands = $this->drupalPostAjaxForm(NULL, array('add_files' => TRUE), array('op' => t('Submit')));
     $new_settings = $this->getDrupalSettings();
     $new_css = $new_settings['ajaxPageState']['css'];
     $new_js = $new_settings['ajaxPageState']['js'];
     // Verify the expected setting was added, both to drupalSettings, and as
     // the first AJAX command.
     $this->assertIdentical($new_settings[$expected['setting_name']], $expected['setting_value'], format_string('Page now has the %setting.', array('%setting' => $expected['setting_name'])));
     $expected_command = new SettingsCommand(array($expected['setting_name'] => $expected['setting_value']), TRUE);
     $this->assertCommand(array_slice($commands, 0, 1), $expected_command->render(), format_string('The settings command was first.'));
     // Verify the expected CSS file was added, both to drupalSettings, and as
     // the second AJAX command for inclusion into the HTML.
     $this->assertEqual($new_css, $original_css + array($expected_css_basename => 1), format_string('Page state now has the %css file.', array('%css' => $expected['css'])));
     $this->assertCommand(array_slice($commands, 1, 1), array('data' => $expected_css_html), format_string('Page now has the %css file.', array('%css' => $expected['css'])));
     // Verify the expected JS file was added, both to drupalSettings, and as
     // the third AJAX command for inclusion into the HTML. By testing for an
     // exact HTML string containing the SCRIPT tag, we also ensure that
     // unexpected JavaScript code, such as a jQuery.extend() that would
     // potentially clobber rather than properly merge settings, didn't
     // accidentally get added.
     $this->assertEqual($new_js, $original_js + array($expected['js'] => 1), format_string('Page state now has the %js file.', array('%js' => $expected['js'])));
     $this->assertCommand(array_slice($commands, 2, 1), array('data' => $expected_js_html), format_string('Page now has the %js file.', array('%js' => $expected['js'])));
 }
コード例 #5
0
 /**
  * Tests that SettingsCommand objects can be constructed and rendered.
  */
 public function testSettingsCommand()
 {
     $command = new SettingsCommand(array('key' => 'value'), TRUE);
     $expected = array('command' => 'settings', 'settings' => array('key' => 'value'), 'merge' => TRUE);
     $this->assertEquals($command->render(), $expected, "SettingsCommand::render() didn't return the expected array.");
 }