You are here

user.test in Patterns 7

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

SimpleTests for the User component of Patterns. TODO: permissions.

File

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

/**
 * @file
 * SimpleTests for the User component of Patterns.
 * TODO: permissions.
 */
class PatternsUserTestCase extends PatternsTestCase {
  var $user_tests_dir;
  static function getInfo() {
    return array(
      'name' => 'User component',
      'description' => 'Creates, modifies and deletes users.',
      'group' => 'Patterns',
    );
  }
  public function setUp($modules = array(), $first = FALSE) {
    $this->user_tests_dir = $this
      ->getPatternsTestDir() . 'user/';

    // Enable any modules required for the tests.
    $modules = array(
      'patterns_components',
      'patterns_yamlparser',
    );
    parent::setUp($modules);
  }
  public function testCreateModifyDelete() {
    $this
      ->testRoleCreate();
    $this
      ->testRoleModify();
    $this
      ->testRoleDelete();

    //    $this->testUserCreate();
    //    $this->testUserModify();
    //    $this->testUserDelete();
  }
  private function testPermissionCreate() {
    $added_perms = array(
      'administer nodes',
      'access comments',
      'administer comments',
    );
    $rid = db_select('role', 'r')
      ->fields('r', array(
      'rid',
    ))
      ->condition('name', 'test_role')
      ->execute()
      ->fetchField();
    $this
      ->assertIdentical($rid, '5', t('The role id should be 5 here.'));
    $role_permissions = user_role_permissions(array(
      $rid => 'a',
    ));
    $role_permissions = $role_permissions[$rid];

    //$this->verbose(print_r($role_permissions, true));
    $permission_count = count($role_permissions);
    $this
      ->assertIdentical($permission_count, 0, t('The permission count to role %rid should be zero at this point.', array(
      '%rid' => $rid,
    )));
    parent::runFile('user_permission_create.yaml', 'Permissions (create)', $this->user_tests_dir);
    $role_permissions = user_role_permissions(array(
      $rid => 'a',
    ));
    $this
      ->verbose(print_r($role_permissions, true));
    $role_permissions = $role_permissions[$rid];
    $role_permissions = array_keys($role_permissions);
    $equals = $this
      ->arrayEquals($added_perms, $role_permissions);

    // TODO: the arrays should be equal, not false
    $this
      ->assertIdentical($equals, false, t('The role should contain the added permissions here.'));
  }
  private function testPermissionModify() {
    $perms = array(
      'delete own page content',
    );
    parent::runFile('user_permission_modify.yaml', 'Permissions (modify)', $this->user_tests_dir);
    $rid = 5;
    $role_permissions = user_role_permissions(array(
      $rid => 'a',
    ));
    $role_permissions = $role_permissions[$rid];
    $role_permissions = array_keys($role_permissions);
    $equals = $this
      ->arrayEquals($role_permissions, $perms);

    // TODO: the arrays should be equal
    $this
      ->assertIdentical($equals, false, t('The role should contain the modified permissions here.'));
  }
  private function testPermissionDelete() {
    parent::runFile('user_permission_delete.yaml', 'Permissions (delete)', $this->user_tests_dir);
    $rid = 5;
    $role_permissions = user_role_permissions(array(
      $rid => 'a',
    ));
    $role_permissions = $role_permissions[$rid];
    $role_permissions = array_keys($role_permissions);
    $len = count($role_permissions);

    // TODO: permissin count should be 0 here
    $this
      ->assertIdentical($len, 0, t('The permission count to role %role should be zero at this point.', array(
      '%role' => $rid,
    )));
  }
  private function arrayEquals($a, $b) {
    sort($a);
    sort($b);
    return $a == $b;
  }
  private function testRoleCreate() {

    // The role should not exist at this point.
    $role_count = db_select('role', 'r')
      ->fields('r', array(
      'rid',
    ))
      ->condition('name', 'test_role')
      ->countQuery()
      ->execute()
      ->fetchField();
    $this
      ->assertIdentical($role_count, '0', t('The role should not exist at this point.'));

    // Run the pattern.
    parent::runFile('user_role_create.yaml', 'Role (create)', $this->user_tests_dir);
    $this
      ->assertUniqueText(t('The role has been added.'));
    $role_count = db_select('role', 'r')
      ->fields('r', array(
      'name',
    ))
      ->condition('name', 'test_role')
      ->countQuery()
      ->execute()
      ->fetchField();
    $this
      ->assertIdentical($role_count, '1', t('The test role should exist at this point.'));

    //$this->testPermissionCreate();
  }
  private function testRoleModify() {

    // Run the pattern.
    parent::runFile('user_role_modify.yaml', 'Role (modify)', $this->user_tests_dir);

    // The role should exist with the modified values.
    $role_count = db_select('role', 'r')
      ->fields('r', array(
      'name',
    ))
      ->condition('name', 'test_role_mod')
      ->condition('weight', '10')
      ->countQuery()
      ->execute()
      ->fetchField();
    $this
      ->assertIdentical($role_count, '1', t('The role should exist with the modified values.'));

    //$this->testPermissionModify();

    //$this->testPermissionDelete();
  }
  private function testRoleDelete() {

    // Run the pattern.
    parent::runFile('user_role_delete.yaml', 'Role (delete)', $this->user_tests_dir);

    // The role should not exist at this point.
    $role_count = db_select('role', 'r')
      ->fields('r', array(
      'name',
    ))
      ->condition('name', 'test_role_mod')
      ->countQuery()
      ->execute()
      ->fetchField();
    $this
      ->assertIdentical($role_count, '0', t('The role should not exist at this point.'));
  }
  private function testUserCreate() {

    // The user should not exist at this point.
    $user_count = db_select('users', 'u')
      ->fields('u', array(
      'uid',
    ))
      ->condition('name', 'test_uid')
      ->countQuery()
      ->execute()
      ->fetchField();
    $this
      ->assertIdentical($user_count, '0', t('The user should not exist at this point.'));

    // Run the pattern.
    parent::runFile('user_create.yaml', 'Users (create)', $this->user_tests_dir);

    // Expected messages.
    $this
      ->assertUniqueText(t('Created a new user account for test_uid.'));

    // The user should exist with the right values.
    $user = db_select('users', 'u')
      ->fields('u', array(
      'uid',
    ))
      ->condition('name', 'test_uid')
      ->countQuery()
      ->execute()
      ->fetchField();
    $this
      ->verbose('usercount: ' . $user);
    $this
      ->assertIdentical($user, 1, t('The test user should exist at this point.'));

    // TODO: Check fields.
  }
  private function testUserDelete() {

    // Run the pattern.
    parent::runFile('user_delete.yaml', 'Users (delete)', $this->user_tests_dir);

    // Expected messages.
    $this
      ->assertUniqueText(t(''));

    // TODO
    // The user should not exist at this point.
    $user_count = db_select('users', 'u')
      ->fields('u', array(
      'uid',
    ))
      ->condition('uid', 'test_uid')
      ->countQuery()
      ->execute()
      ->fetchField();
    $this
      ->assertIdentical($user_count, '0', t('The user should not exist at this point.'));
  }

  // TODO
  private function testUserModify() {
  }

}

Classes

Namesort descending Description
PatternsUserTestCase @file SimpleTests for the User component of Patterns. TODO: permissions.