You are here

shortcut.test in Patterns 7

Same filename and directory in other branches
  1. 7.2 tests/shortcut/shortcut.test

SimpleTests for the Shortcut component of Patterns.

File

tests/shortcut/shortcut.test
View source
<?php

/**
 * @file
 * SimpleTests for the Shortcut component of Patterns.
 *
 */
class PatternsShortcutTestCase extends PatternsTestCase {
  var $shortcut_tests_dir;
  public static function getInfo() {
    return array(
      'name' => 'shortcut component',
      'description' => 'Tests shortcut pattern',
      'group' => 'Patterns',
    );
  }
  public function setUp($modules = array(), $first = FALSE) {
    $this->shortcut_tests_dir = $this
      ->getPatternsTestDir() . 'shortcut/';

    // Enable any modules required for the tests.
    $modules = array(
      'patterns_components',
      'patterns_yamlparser',
    );
    parent::setUp($modules);

    // Create a role with the given permission set, if any.
    $rid = FALSE;
    $permissions = array(
      'administer site configuration',
      'access administration pages',
      'administer patterns',
      'administer shortcuts',
      'switch shortcut sets',
      'customize shortcut links',
    );
    if ($permissions) {
      $rid = $this
        ->drupalCreateRole($permissions);
      if (!$rid) {
        return FALSE;
      }
    }

    // Create a user assigned to that role.
    $edit = array();
    $edit['name'] = 'someone';
    $edit['mail'] = $edit['name'] . '@example.com';
    $edit['pass'] = user_password();
    $edit['status'] = 1;
    if ($rid) {
      $edit['roles'] = array(
        $rid => $rid,
      );
    }
    $account = user_save(drupal_anonymous_user(), $edit);
    $this
      ->assertTrue(!empty($account->uid), t('User created with name %name and pass %pass', array(
      '%name' => $edit['name'],
      '%pass' => $edit['pass'],
    )), t('User login'));
    if (empty($account->uid)) {
      return FALSE;
    }

    // Add the raw password so that we can log in as this user.
    $account->pass_raw = $edit['pass'];
    $this->admin_user = $account;

    // "someone" login
    $this
      ->drupalLogin($this->admin_user);
  }

  /**
   * Test the:
   *
   *    * add a new set 'new_set_fortest'
   *    * add a new shortcut link to the new set 'new_set_fortest'  which add before
   *    * edit the link, which add before
   *    * switch set for user
   *    * delete the link, which just edit
   *    * change the set "new_set_fortest" name to "newer_set_fortest"
   *    * delete the set "newer_set_fortest"
   */
  public function test_shortcut() {

    //for add a new set 'new_set_fortest'
    parent::runFile('shortcut_add_set.yaml', 'add new shortcut set (create)', $this->shortcut_tests_dir);
    $query_add_set = db_select('shortcut_set', 'ss')
      ->fields('ss', array(
      'set_name',
      'title',
    ))
      ->condition('ss.title', 'new_set_fortest')
      ->execute()
      ->fetchAssoc();
    $this
      ->assertTrue($query_add_set, t('Adding a new set \'new_set_fortest\' works fine.'));
    $num = count($query_add_set);
    $this
      ->assert('pass', 'the num of query db is ' . $num);
    foreach ($query_add_set as $key => $value) {
      $this
        ->assert('pass', $key . ' is ' . $value);
    }
    $this
      ->drupalGet('admin/config/user-interface/shortcut');
    $this
      ->assertResponse(200);
    $this
      ->assertRaw('<td>new_set_fortest</td>', t('new_set_fortest is built successful.'));

    //for add a new shortcut link to the new set which add before
    parent::runFile('shortcut_add_link.yaml', 'add a link to a shortcut set (create)', $this->shortcut_tests_dir);
    $query = db_select('menu_links', 'ml')
      ->fields('ml', array(
      'menu_name',
      'mlid',
      'link_path',
      'link_title',
    ))
      ->condition('ml.menu_name', $query_add_set['set_name'])
      ->condition('ml.link_title', 'link_admcont')
      ->condition('ml.link_path', 'admin/content')
      ->execute()
      ->fetchAssoc();
    $this
      ->assertTrue($query, t('Adding shortcut link works fine.'));
    $num = count($query);
    $this
      ->assert('pass', 'the num of query db is ' . $num);
    foreach ($query as $key => $value) {
      $this
        ->assert('pass', $key . ' is ' . $value);
    }
    $this
      ->drupalGet('admin/config/user-interface/shortcut/' . $query_add_set['set_name']);
    $this
      ->assertResponse(200);
    $this
      ->assertRaw('admin/content">link_admcont</a></td>', t('the new link:link_admcont(admin/content) is built successful.'));

    //for edit the link, which add before
    parent::runFile('shortcut_edit_link.yaml', 'edit a link of a shortcut set (modify)', $this->shortcut_tests_dir);
    $query = db_select('menu_links', 'ml')
      ->fields('ml', array(
      'menu_name',
      'mlid',
      'link_path',
      'link_title',
    ))
      ->condition('ml.menu_name', $query_add_set['set_name'])
      ->condition('ml.link_title', 'link_nodadd')
      ->condition('ml.link_path', 'node/add')
      ->execute()
      ->fetchAssoc();
    $this
      ->assertTrue($query, t('Edit shortcut link works fine.'));
    $num = count($query);
    $this
      ->assert('pass', 'the num of query db is ' . $num);
    foreach ($query as $key => $value) {
      $this
        ->assert('pass', $key . ' is ' . $value);
    }
    $this
      ->drupalGet('admin/config/user-interface/shortcut/' . $query_add_set['set_name']);
    $this
      ->assertResponse(200);
    $this
      ->assertNoRaw('admin/content">link_admcont</a></td>', t('the old link:link_adncont(admin/content) has changed.'));
    $this
      ->assertRaw('node/add">link_nodadd</a></td>', t('the new link:link_nodadd(node/add) has been built successful.'));

    //for switch user's shortcut set
    parent::runFile('shortcut_set_switch.yaml', 'switch shortcut set for users (modify)', $this->shortcut_tests_dir);
    $queryuser = db_select('users', 'u')
      ->fields('u', array(
      'uid',
    ))
      ->condition('u.name', 'someone')
      ->execute()
      ->fetchField();
    $query = db_select('shortcut_set_users', 'ssu')
      ->fields('ssu', array(
      'set_name',
      'uid',
    ))
      ->condition('ssu.uid', $queryuser)
      ->condition('ssu.set_name', $query_add_set['set_name'])
      ->execute()
      ->fetchAssoc();
    $this
      ->assertTrue($query, t('Switching user\'s shortcut set works fine.'));
    $num = count($query);
    $this
      ->assert('pass', 'the num of query db is ' . $num);
    foreach ($query as $key => $value) {
      $this
        ->assert('pass', $key . ' is ' . $value);
    }
    $this
      ->drupalGet('user/' . $queryuser . '/shortcuts');
    $this
      ->assertResponse(200);
    $this
      ->assertRaw('value="' . $query_add_set['set_name'] . '" checked="checked"', t('user "someone"\'s shortcut_set is ' . $query_add_set['title'] . ' now'));

    //TODO it seems the toolbar and shortcut can not be enabled in the simpletest, why?

    //$this->assertRaw('node/add">link_nodadd</a></li>', t('user "someone"\'s shortcut_set has the link_nodadd now'));

    //for delete the link, which just edit
    parent::runFile('shortcut_delete_link.yaml', 'delete a link from a shortcut set (modify)', $this->shortcut_tests_dir);
    $query = db_select('menu_links', 'ml')
      ->fields('ml', array(
      'menu_name',
      'mlid',
      'link_path',
      'link_title',
    ))
      ->condition('ml.menu_name', $query_add_set['set_name'])
      ->condition('ml.link_title', 'link_nodadd')
      ->condition('ml.link_path', 'node/add')
      ->execute()
      ->fetchAssoc();
    $this
      ->assertFalse($query, t('Delete shortcut link works fine.'));
    $this
      ->drupalGet('admin/config/user-interface/shortcut/' . $query_add_set['set_name']);
    $this
      ->assertResponse(200);
    $this
      ->assertNoRaw('node/add">link_nodadd</a></td>', t('the new link:link_nodadd(node/add) has been deleted.'));

    //for change the set "new_set_fortest" name to "newer_set_fortest"
    parent::runFile('shortcut_edit_set.yaml', 'edit the shortcut set name(modify)', $this->shortcut_tests_dir);
    $query = db_select('shortcut_set', 'ss')
      ->fields('ss', array(
      'set_name',
      'title',
    ))
      ->condition('ss.title', 'newer_set_fortest')
      ->condition('ss.set_name', $query_add_set['set_name'])
      ->execute()
      ->fetchAssoc();
    $this
      ->assertTrue($query, t('Editing set works fine.'));
    $num = count($query);
    $this
      ->assert('pass', 'the num of query db is ' . $num);
    foreach ($query as $key => $value) {
      $this
        ->assert('pass', $key . ' is ' . $value);
    }
    $this
      ->drupalGet('admin/config/user-interface/shortcut');
    $this
      ->assertResponse(200);
    $this
      ->assertRaw('<td>newer_set_fortest</td>', t('new_set_fortest has been changed to newer_set_fortest.'));

    //for delete the set, which add before
    parent::runFile('shortcut_delete_set.yaml', 'delete a shortcut set (delete)', $this->shortcut_tests_dir);
    $query = db_select('shortcut_set', 'ss')
      ->fields('ss', array(
      'set_name',
      'title',
    ))
      ->condition('ss.title', 'newer_set_fortest')
      ->condition('ss.set_name', $query_add_set['set_name'])
      ->execute()
      ->fetchAssoc();
    $this
      ->assertFalse($query, t('Deleting set works fine.'));
    $this
      ->drupalGet('admin/config/user-interface/shortcut');
    $this
      ->assertResponse(200);
    $this
      ->assertNoRaw('<td>newer_set_fortest</td>', t('newer_set_fortest has been deleted.'));
    $this
      ->drupalGet('user/' . $queryuser . '/shortcuts');
    $this
      ->assertResponse(200);
  }

}

Classes

Namesort descending Description
PatternsShortcutTestCase @file SimpleTests for the Shortcut component of Patterns.