You are here

role_export.test in Role Export 6

Test Role Export.

File

role_export.test
View source
<?php

/**
 * @file
 * Test Role Export.
 */
class RoleExportTestCase extends DrupalWebTestCase {
  protected $privileged_user;
  public static function getInfo() {
    return array(
      'name' => 'role_export',
      'description' => 'Ensure that role_export functions properly.',
      'group' => 'Features',
    );
  }
  public function setUp() {
    parent::setUp('features', 'role_export');

    // Make sure Features is enabled, since parent::setUp() doesn't check this for us
    $this
      ->assertTrue(module_exists('features'), "features.module isn't installed");
  }
  public function testExport() {

    // Create a user with permission to create Roles
    $u = $this
      ->drupalCreateUser(array(
      'access administration pages',
      'administer permissions',
    ));
    $this
      ->drupalLogin($u);

    // Create a Role
    $edit = array(
      'name' => 'Test Role',
      'machine_name' => 'test_role_with_different_machine_name',
    );
    $this
      ->drupalPost('admin/user/roles', $edit, 'Add role');
    $this
      ->assertRaw("The role has been added.", "Unable to add test role.");

    // Make sure the machine_name was correctly hashed into an rid
    $roles = user_roles();
    $this
      ->assertTrue(isset($roles[924007658]), "Test Role wasn't added, or has incorrect hashed machine_name.");

    // Export a Feature
    $this
      ->assertFalse(module_exists('test_role_feature'), "The test Feature export module is already enabled.");
    drush_set_context('DRUSH_AFFIRMATIVE', TRUE);
    drush_features_export('test_role_feature', 'role_export:test_role_with_different_machine_name');
    $this
      ->assertTrue(is_dir('sites/all/modules/test_role_feature'), "Failed to export Feature.");

    // Check contents of exported Feature
    module_load_include('inc', 'test_role_feature', 'test_role_feature.features.role_export');
    $this
      ->assertTrue(function_exists('test_role_feature_role_export_defaults'), "Function test_role_feature_role_export_defaults doesn't appear in exported Feature.");
    $defaults = test_role_feature_role_export_defaults();
    $this
      ->assertTrue(isset($defaults['test_role_with_different_machine_name']), "No default defined for test_role_with_different_machine_name.");
    $this
      ->assertEqual($defaults['test_role_with_different_machine_name']['rid'], 924007658, "Incorrect exported rid.");
    $this
      ->assertEqual($defaults['test_role_with_different_machine_name']['name'], 'Test Role', "Incorrect exported name.");
    $this
      ->assertEqual($defaults['test_role_with_different_machine_name']['machine_name'], 'test_role_with_different_machine_name', "Incorrect exported machine_name.");
  }
  public function testImport() {
    $this
      ->assertTrue(is_dir('sites/all/modules/test_role_feature'), "Can't find Feature exported in previous test.");

    // Activate the Feature
    $this
      ->assertFalse(module_exists('test_role_feature'), "The test Feature export module is already enabled.");
    drupal_install_modules(array(
      'test_role_feature',
    ));
    $this
      ->assertTrue(module_exists('test_role_feature'), "The test Feature export module didn't install.");

    // Our Feature requires a revert, since it's a "faux-exportable"
    features_revert(array(
      'test_role_feature' => array(
        'role_export',
      ),
    ));

    // Make sure the exported role was imported and has the correct rid and name
    $roles = user_roles();
    $this
      ->assertTrue(isset($roles[924007658]), "The test Feature export didn't create a Role, or didn't create a Role with the correct hashed machine_name.");
    $this
      ->assertEqual($roles[924007658], 'Test Role', "The test Feature export didn't create a Role with the correct display name.");
  }

}

Classes

Namesort descending Description
RoleExportTestCase @file Test Role Export.