You are here

asaf.test in Asaf (ajax submit for any form) 7

Same filename and directory in other branches
  1. 8 tests/asaf.test

File

tests/asaf.test
View source
<?php

class AsafBaseTestCase extends DrupalWebTestCase {
  function setUp() {
    parent::setUp(array(
      'devel',
      'asaf',
      'asaf_example',
    ));
  }
  protected function getButtons($formId = NULL) {
    $buttons = array();
    $xpath = '//input[@type="submit"]';
    if (isset($formId)) {
      $xpath = '//form[@id="' . $formId . '"]' . $xpath;
    }
    $elements = $this
      ->xpath($xpath);
    foreach ($elements as $element) {
      $id = (string) $element['id'];
      $buttons[$id] = $id;
    }
    return $buttons;
  }

  /*
   * Initialize form area wrapper using regexp instead of js.
   */
  protected function initAsafFormWrapper() {
    $this->content = preg_replace('/(<form(.*)data-asaf-area-wrapper-id=\\"(.*?)\\"([\\s\\w\\W]*)<\\/form>)/i', '<div id="\\3">\\1</div>', $this->content);
  }

}
class AsafCommonTests extends AsafBaseTestCase {
  public static function getInfo() {
    return array(
      'name' => 'Asaf common tests',
      'description' => 'Asaf tests',
      'group' => 'Asaf',
    );
  }

  /*
   * Check ajax handling
   */
  function testAjaxButtons() {

    // Simples form
    variable_set('asaf_forms', "asaf_example_gui_simplest");
    $this
      ->drupalGet('examples/asaf_example/gui/simplest');
    $settings = $this
      ->drupalGetSettings();
    $buttonIds = array_keys($this
      ->getButtons('asaf-example-gui-simplest'));
    $this
      ->assertTrue(isset($settings['ajax'][reset($buttonIds)]), t('Submit button is ajaxified'));

    // Check single button enabling
    variable_set('asaf_forms', "asaf_example_gui_simplest\nasaf_example_gui_buttons@actions][decrement");
    $this
      ->drupalGet('examples/asaf_example/gui/buttons');
    $settings = $this
      ->drupalGetSettings();
    $buttons = $this
      ->getButtons('asaf-example-gui-buttons');
    $enabledButtons = array_keys(array_intersect_key($buttons, $settings['ajax']));
    $this
      ->assertTrue(count($enabledButtons) == 1 && strpos(reset($enabledButtons), 'actions-decrement') !== FALSE, t('Single button ajaxified correctly'));

    // Check excluded button
    variable_set('asaf_forms', "asaf_example_gui_simplest\nasaf_example_gui_buttons@-actions][multiply");
    $this
      ->drupalGet('examples/asaf_example/gui/buttons');
    $settings = $this
      ->drupalGetSettings();
    $buttons = $this
      ->getButtons('asaf-example-gui-buttons');
    $excludedButtons = array_keys(array_diff_key($buttons, $settings['ajax']));
    $this
      ->assertTrue(reset($excludedButtons) == 'edit-multiply', t('Button excluded correctly'));

    // Check ajax url when pagecache is enabled
    variable_set('cache', TRUE);
    $this
      ->drupalGet('examples/asaf_example/api/simplest');
    $settings = $this
      ->drupalGetSettings();
    $buttonIds = array_keys($this
      ->getButtons('asaf-example-api-simplest'));
    $this
      ->assertTrue($settings['ajax'][reset($buttonIds)]['url'] == '/system/ajax/asaf/pagecache', t('Correct ajax url when pagecache is enabled'));
  }

  /*
   * Check ajax handling
   */
  function testFormSubmit() {
    $this
      ->drupalGet('examples/asaf_example/api/simplest');
    $this
      ->initAsafFormWrapper();
    $buttonIds = array_keys($this
      ->getButtons('asaf-example-api-simplest'));
    $buttonId = reset($buttonIds);
    $settings = $this
      ->drupalGetSettings();
    $ajax_setting = $settings['ajax'][$buttonId];
    $this
      ->drupalPostAJAX(NULL, array(), 'op', NULL, array(), array(), NULL, $ajax_setting);
    $this
      ->assertText('Name field is required.', t('Checking ajax validate'));
    $edit = array(
      'name' => t('Anka'),
    );
    $this
      ->drupalGet('examples/asaf_example/api/simplest');
    $this
      ->initAsafFormWrapper();
    $buttonIds = array_keys($this
      ->getButtons('asaf-example-api-simplest'));
    $buttonId = reset($buttonIds);
    $settings = $this
      ->drupalGetSettings();
    $ajax_setting = $settings['ajax'][$buttonId];
    $this
      ->drupalPostAJAX(NULL, $edit, 'op', NULL, array(), array(), NULL, $ajax_setting);
    $this
      ->assertText('Specified name: Anka', t('Checking ajax submit'));
  }

}
class AsafUnitTests extends DrupalUnitTestCase {
  public static function getInfo() {
    return array(
      'name' => 'Asaf infrastructure tests',
      'description' => '',
      'group' => 'Asaf',
    );
  }
  function testNeededFilesList() {
    $asafPath = drupal_get_path('module', 'asaf');
    $list = asaf_get_needed_files_list(array(
      $asafPath . '/tests/asaf.test',
    ));
    $file = $asafPath . '/tests/asaf.test';
    $this
      ->assertTrue(array_search($file, $list) !== FALSE, t('Attach a simple file (repeater mode)'));
    $list = asaf_get_needed_files_list(array(
      'user',
    ));
    $condition = array_search('modules/user/user.module', $list) !== FALSE && array_search('modules/user/user.tokens.inc', $list) !== FALSE && array_search('modules/user/user.admin.inc', $list) !== FALSE && array_search('modules/user/user.pages.inc', $list) !== FALSE && count($list) == 4;
    $this
      ->assertTrue($condition, t('Detecting module includes'));
  }

}