Check theme and ajax functions and commands.

Fichier

tests/advagg.test, line 1440

Classe

AdvAggThemeTestCase
Unit tests for the Theme API.

Code

public function testCssOverride() {
    // Ensures a theme's .info file is able to override a module CSS file from
    // being added to the page.
    advagg_test_reset_statics();
    // Reuse the same page as in testPreprocessForSuggestions(). We're testing
    // what is output to the HTML HEAD based on what is in a theme's .info file,
    // so it doesn't matter what page we get, as long as it is themed with the
    // test theme. First we test with CSS aggregation disabled.
    $GLOBALS['conf']['preprocess_css'] = 0;
    variable_set('preprocess_css', 0);
    $this->drupalGet('theme-test/suggestion');
    $this->assertNoText('system.base.css', 'The theme\'s .info file is able to override a module CSS file from being added to the page.');
    // Also test with aggregation enabled, simply ensuring no PHP errors are
    // triggered during drupal_build_css_cache() when a source file doesn't
    // exist. Then allow remaining tests to continue with aggregation disabled
    // by default.
    variable_set('preprocess_css', 1);
    $this->drupalGet('theme-test/suggestion');
    variable_set('preprocess_css', 0);
    //
    // Test ajax and js settings.
    advagg_test_reset_statics();
    $commands = $this->drupalGetAJAX('ajax-test/render');
    // Verify that there is a command to load settings added with
    // drupal_add_js().
    $expected = array(
        'command' => 'settings',
        'settings' => array(
            'basePath' => base_path(),
            'ajax' => 'test',
        ),
    );
    $this->assertCommand($commands, $expected, t('ajax_render() loads settings added with drupal_add_js().'));
    // Verify that Ajax settings are loaded for #type 'link'.
    $this->drupalGet('ajax-test/link');
    $settings = $this->drupalGetSettings();
    $this->assertEqual($settings['ajax']['ajax-link']['url'], url('filter/tips'));
    $this->assertEqual($settings['ajax']['ajax-link']['wrapper'], 'block-system-main');
    //
    // Test behavior of ajax_render_error().
    // Reset before each test.
    advagg_test_reset_statics();
    // Verify default error message.
    $commands = $this->drupalGetAJAX('ajax-test/render-error');
    $expected = array(
        'command' => 'alert',
        'text' => t('An error occurred while handling the request: The server received invalid input.'),
    );
    $this->assertCommand($commands, $expected, t('ajax_render_error() invokes alert command.'));
    // Verify custom error message.
    $edit = array(
        'message' => 'Custom error message.',
    );
    $commands = $this->drupalGetAJAX('ajax-test/render-error', array(
        'query' => $edit,
    ));
    $expected = array(
        'command' => 'alert',
        'text' => $edit['message'],
    );
    $this->assertCommand($commands, $expected, t('Custom error message is output.'));
    //
    // Test that new JavaScript and CSS files added during an AJAX request are
    // returned.
    // Reset before each test.
    advagg_test_reset_statics();
    $expected = array(
        'setting_name' => 'ajax_forms_test_lazy_load_form_submit',
        'setting_value' => 'executed',
        'css' => drupal_get_path('module', 'system') . '/system.admin.css',
        'js' => drupal_get_path('module', 'system') . '/system.js',
    );
    // @todo D8: Add a drupal_css_defaults() helper function.
    $expected_css_render_array = advagg_get_css(array(
        $expected['css'] => array(
            'type' => 'file',
            'group' => CSS_DEFAULT,
            'weight' => 0,
            'every_page' => FALSE,
            'media' => 'all',
            'preprocess' => TRUE,
            'data' => $expected['css'],
            'browsers' => array(
                'IE' => TRUE,
                '!IE' => TRUE,
            ),
        ),
    ), TRUE);
    $expected_css_html = drupal_render($expected_css_render_array);
    $expected_js_render_array = advagg_get_js('header', array(
        $expected['js'] => drupal_js_defaults($expected['js']),
    ), TRUE);
    $expected_js_html = drupal_render($expected_js_render_array);
    // Get the base page.
    $this->drupalGet('ajax_forms_test_lazy_load_form');
    $original_settings = $this->drupalGetSettings();
    $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']]), t('Page originally lacks the %setting, as expected.', array(
        '%setting' => $expected['setting_name'],
    )));
    $this->assertTrue(!isset($original_settings[$expected['css']]), t('Page originally lacks the %css file, as expected.', array(
        '%css' => $expected['css'],
    )));
    $this->assertTrue(!isset($original_settings[$expected['js']]), t('Page originally lacks the %js file, as expected.', array(
        '%js' => $expected['js'],
    )));
    // Submit the AJAX request without triggering files getting added.
    $commands = $this->drupalPostAJAX(NULL, array(
        'add_files' => FALSE,
    ), array(
        'op' => t('Submit'),
    ));
    $new_settings = $this->drupalGetSettings();
    // Verify the setting was not added when not expected.
    $this->assertTrue(!isset($new_settings['setting_name']), t('Page still lacks the %setting, as expected.', array(
        '%setting' => $expected['setting_name'],
    )));
    // Verify a settings command does not add CSS or scripts to Drupal.settings
    // 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, t('Page state still lacks the %css and %js files, as expected.', array(
        '%css' => $expected['css'],
        '%js' => $expected['js'],
    )));
    $this->assertFalse($found_markup_command, t('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->drupalPostAJAX(NULL, array(
        'add_files' => TRUE,
    ), array(
        'op' => t('Submit'),
    ));
    $new_settings = $this->drupalGetSettings();
    $new_css = $new_settings['ajaxPageState']['css'];
    $new_js = $new_settings['ajaxPageState']['js'];
    // Verify the expected setting was added.
    $this->assertIdentical($new_settings[$expected['setting_name']], $expected['setting_value'], t('Page now has the %setting.', array(
        '%setting' => $expected['setting_name'],
    )));
    // Verify the expected CSS file was added, both to Drupal.settings, and as
    // an AJAX command for inclusion into the HTML.
    $this->assertEqual($new_css, $original_css + array(
        $expected['css'] => 1,
    ), t('Page state now has the %css file.', array(
        '%css' => $expected['css'],
    )));
    $this->assertCommand($commands, array(
        'data' => $expected_css_html,
    ), t('Page now has the %css file.', array(
        '%css' => $expected['css'],
    )));
    // Verify the expected JS file was added, both to Drupal.settings, and as
    // an 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,
    ), t('Page state now has the %js file.', array(
        '%js' => $expected['js'],
    )));
    $this->assertCommand($commands, array(
        'data' => $expected_js_html,
    ), t('Page now has the %js file.', array(
        '%js' => $expected['js'],
    )));
    //
    // Tests that overridden CSS files are not added during lazy load.
    // Reset before each test.
    advagg_test_reset_statics();
    // The test theme overrides system.base.css without an implementation,
    // thereby removing it.
    theme_enable(array(
        'test_theme',
    ));
    variable_set('theme_default', 'test_theme');
    // This gets the form, and emulates an Ajax submission on it, including
    // adding markup to the HEAD and BODY for any lazy loaded JS/CSS files.
    $this->drupalPostAJAX('ajax_forms_test_lazy_load_form', array(
        'add_files' => TRUE,
    ), array(
        'op' => t('Submit'),
    ));
    // Verify that the resulting HTML does not load the overridden CSS file.
    // We add a "?" to the assertion, because Drupal.settings may include
    // information about the file; we only really care about whether it appears
    // in a LINK or STYLE tag, for which Drupal always adds a query string for
    // cache control.
    $this->assertNoText('system.base.css?', 'Ajax lazy loading does not add overridden CSS files.');
    //
    // Test the various Ajax Commands.
    // Reset before each test.
    advagg_test_reset_statics();
    $form_path = 'ajax_forms_test_ajax_commands_form';
    $web_user = $this->drupalCreateUser(array(
        'access content',
    ));
    $this->drupalLogin($web_user);
    $edit = array();
    // Tests the 'after' command.
    $commands = $this->drupalPostAJAX($form_path, $edit, array(
        'op' => t("AJAX 'After': Click to put something after the div"),
    ));
    $expected = array(
        'command' => 'insert',
        'method' => 'after',
        'data' => 'This will be placed after',
    );
    $this->assertCommand($commands, $expected, "'after' AJAX command issued with correct data");
    // Tests the 'alert' command.
    $commands = $this->drupalPostAJAX($form_path, $edit, array(
        'op' => t("AJAX 'Alert': Click to alert"),
    ));
    $expected = array(
        'command' => 'alert',
        'text' => 'Alert',
    );
    $this->assertCommand($commands, $expected, "'alert' AJAX Command issued with correct text");
    // Tests the 'append' command.
    $commands = $this->drupalPostAJAX($form_path, $edit, array(
        'op' => t("AJAX 'Append': Click to append something"),
    ));
    $expected = array(
        'command' => 'insert',
        'method' => 'append',
        'data' => 'Appended text',
    );
    $this->assertCommand($commands, $expected, "'append' AJAX command issued with correct data");
    // Tests the 'before' command.
    $commands = $this->drupalPostAJAX($form_path, $edit, array(
        'op' => t("AJAX 'before': Click to put something before the div"),
    ));
    $expected = array(
        'command' => 'insert',
        'method' => 'before',
        'data' => 'Before text',
    );
    $this->assertCommand($commands, $expected, "'before' AJAX command issued with correct data");
    // Tests the 'changed' command.
    $commands = $this->drupalPostAJAX($form_path, $edit, array(
        'op' => t("AJAX changed: Click to mark div changed."),
    ));
    $expected = array(
        'command' => 'changed',
        'selector' => '#changed_div',
    );
    $this->assertCommand($commands, $expected, "'changed' AJAX command issued with correct selector");
    // Tests the 'changed' command using the second argument.
    $commands = $this->drupalPostAJAX($form_path, $edit, array(
        'op' => t("AJAX changed: Click to mark div changed with asterisk."),
    ));
    $expected = array(
        'command' => 'changed',
        'selector' => '#changed_div',
        'asterisk' => '#changed_div_mark_this',
    );
    $this->assertCommand($commands, $expected, "'changed' AJAX command (with asterisk) issued with correct selector");
    // Tests the 'css' command.
    $commands = $this->drupalPostAJAX($form_path, $edit, array(
        'op' => t("Set the '#box' div to be blue."),
    ));
    $expected = array(
        'command' => 'css',
        'selector' => '#css_div',
        'argument' => array(
            'background-color' => 'blue',
        ),
    );
    $this->assertCommand($commands, $expected, "'css' AJAX command issued with correct selector");
    // Tests the 'data' command.
    $commands = $this->drupalPostAJAX($form_path, $edit, array(
        'op' => t("AJAX data command: Issue command."),
    ));
    $expected = array(
        'command' => 'data',
        'name' => 'testkey',
        'value' => 'testvalue',
    );
    $this->assertCommand($commands, $expected, "'data' AJAX command issued with correct key and value");
    // Tests the 'invoke' command.
    $commands = $this->drupalPostAJAX($form_path, $edit, array(
        'op' => t("AJAX invoke command: Invoke addClass() method."),
    ));
    $expected = array(
        'command' => 'invoke',
        'method' => 'addClass',
        'arguments' => array(
            'error',
        ),
    );
    $this->assertCommand($commands, $expected, "'invoke' AJAX command issued with correct method and argument");
    // Tests the 'html' command.
    $commands = $this->drupalPostAJAX($form_path, $edit, array(
        'op' => t("AJAX html: Replace the HTML in a selector."),
    ));
    $expected = array(
        'command' => 'insert',
        'method' => 'html',
        'data' => 'replacement text',
    );
    $this->assertCommand($commands, $expected, "'html' AJAX command issued with correct data");
    // Tests the 'insert' command.
    $commands = $this->drupalPostAJAX($form_path, $edit, array(
        'op' => t("AJAX insert: Let client insert based on #ajax['method']."),
    ));
    $expected = array(
        'command' => 'insert',
        'data' => 'insert replacement text',
    );
    $this->assertCommand($commands, $expected, "'insert' AJAX command issued with correct data");
    // Tests the 'prepend' command.
    $commands = $this->drupalPostAJAX($form_path, $edit, array(
        'op' => t("AJAX 'prepend': Click to prepend something"),
    ));
    $expected = array(
        'command' => 'insert',
        'method' => 'prepend',
        'data' => 'prepended text',
    );
    $this->assertCommand($commands, $expected, "'prepend' AJAX command issued with correct data");
    // Tests the 'remove' command.
    $commands = $this->drupalPostAJAX($form_path, $edit, array(
        'op' => t("AJAX 'remove': Click to remove text"),
    ));
    $expected = array(
        'command' => 'remove',
        'selector' => '#remove_text',
    );
    $this->assertCommand($commands, $expected, "'remove' AJAX command issued with correct command and selector");
    // Tests the 'restripe' command.
    $commands = $this->drupalPostAJAX($form_path, $edit, array(
        'op' => t("AJAX 'restripe' command"),
    ));
    $expected = array(
        'command' => 'restripe',
        'selector' => '#restripe_table',
    );
    $this->assertCommand($commands, $expected, "'restripe' AJAX command issued with correct selector");
    // Tests the 'settings' command.
    $commands = $this->drupalPostAJAX($form_path, $edit, array(
        'op' => t("AJAX 'settings' command"),
    ));
    $expected = array(
        'command' => 'settings',
        'settings' => array(
            'ajax_forms_test' => array(
                'foo' => 42,
            ),
        ),
    );
    $this->assertCommand($commands, $expected, "'settings' AJAX command issued with correct data");
    // Tests the 'add_css' command.
    $commands = $this->drupalPostAJAX($form_path, $edit, array(
        'op' => t("AJAX 'add_css' command"),
    ));
    $expected = array(
        'command' => 'add_css',
        'data' => 'my/file.css',
    );
    $this->assertCommand($commands, $expected, "'add_css' AJAX command issued with correct data");
}