You are here

public function EntityMetadataIntegrationTestCase::testUserProperties in Entity API 7

Test all properties of a user.

File

./entity.test, line 1788
Entity CRUD API tests.

Class

EntityMetadataIntegrationTestCase
Tests provided entity property info of the core modules.

Code

public function testUserProperties() {
  $account = $this
    ->drupalCreateUser(array(
    'access user profiles',
    'change own username',
  ));
  $account->login = REQUEST_TIME;
  $account->access = REQUEST_TIME;
  $wrapper = entity_metadata_wrapper('user', $account);
  foreach ($wrapper as $key => $value) {
    $this
      ->assertValue($wrapper, $key);
  }

  // Test property view access.
  $unpriviledged_user = $this
    ->drupalCreateUser(array(
    'access user profiles',
  ));
  $admin_user = $this
    ->drupalCreateUser(array(
    'administer users',
  ));
  $this
    ->assertTrue($wrapper
    ->access('view', $unpriviledged_user), 'Unpriviledged account can view the user.');
  $this
    ->assertFalse($wrapper
    ->access('edit', $unpriviledged_user), 'Unpriviledged account can not edit the user.');
  $view_access = array(
    'name',
    'url',
    'edit_url',
    'created',
  );
  foreach ($view_access as $property_name) {
    $this
      ->assertTrue($wrapper->{$property_name}
      ->access('view', $unpriviledged_user), "Unpriviledged user can view the {$property_name} property.");
  }
  $view_denied = array(
    'mail',
    'last_access',
    'last_login',
    'roles',
    'status',
    'theme',
  );
  foreach ($view_denied as $property_name) {
    $this
      ->assertFalse($wrapper->{$property_name}
      ->access('view', $unpriviledged_user), "Unpriviledged user can not view the {$property_name} property.");
    $this
      ->assertTrue($wrapper->{$property_name}
      ->access('view', $admin_user), "Admin user can view the {$property_name} property.");
  }

  // Test property edit access.
  $edit_own_allowed = array(
    'name',
    'mail',
  );
  foreach ($edit_own_allowed as $property_name) {
    $this
      ->assertTrue($wrapper->{$property_name}
      ->access('edit', $account), "Account owner can edit the {$property_name} property.");
  }
  $this
    ->assertTrue($wrapper->roles
    ->access('view', $account), "Account owner can view their own roles.");
  $edit_denied = array(
    'last_access',
    'last_login',
    'created',
    'roles',
    'status',
    'theme',
  );
  foreach ($edit_denied as $property_name) {
    $this
      ->assertFalse($wrapper->{$property_name}
      ->access('edit', $account), "Account owner cannot edit the {$property_name} property.");
    $this
      ->assertTrue($wrapper->{$property_name}
      ->access('edit', $admin_user), "Admin user can edit the {$property_name} property.");
  }
}