Skip to content
EntityUnitTestBase.php 2.89 KiB
Newer Older
 * Contains \Drupal\system\Tests\Entity\EntityUnitTestBase.
 */

namespace Drupal\system\Tests\Entity;

use Drupal\simpletest\DrupalUnitTestBase;
use Drupal\Core\Entity\EntityInterface;

/**
 * Defines an abstract test base for entity unit tests.
 */
abstract class EntityUnitTestBase extends DrupalUnitTestBase {
  public static $modules = array('entity', 'user', 'system', 'field', 'text', 'filter', 'entity_test');
   * @var \Drupal\Core\Entity\EntityManagerInterface
   */
  protected $entityManager;

  /**
   * The state service.
   *
   * @var \Drupal\Core\KeyValueStore\StateInterface
    $this->entityManager = $this->container->get('entity.manager');
    $this->state = $this->container->get('state');

    $this->installSchema('user', array('users', 'users_roles'));
    $this->installSchema('system', 'sequences');
    $this->installSchema('entity_test', 'entity_test');
    $this->installConfig(array('field'));
  }

  /**
   * Creates a user.
   *
   * @param array $values
   *   (optional) The values used to create the entity.
   * @param array $permissions
   *   (optional) Array of permission names to assign to user. The
   *   users_roles tables must be installed before this can be used.
   *   The created user entity.
   */
  protected function createUser($values = array(), $permissions = array()) {
    if ($permissions) {
      // Create a new role and apply permissions to it.
      $role = entity_create('user_role', array(
        'id' => strtolower($this->randomName(8)),
        'label' => $this->randomName(8),
      ));
      $role->save();
      user_role_grant_permissions($role->id(), $permissions);
    }

    $account = entity_create('user', $values + array(
      'name' => $this->randomName(),
      'status' => 1,
    ));
    $account->enforceIsNew();
    $account->save();
    return $account;
  }

  /**
   * Reloads the given entity from the storage and returns it.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity to be reloaded.
   *
   * @return \Drupal\Core\Entity\EntityInterface
   *   The reloaded entity.
   */
  protected function reloadEntity(EntityInterface $entity) {
    $controller = $this->entityManager->getStorageController($entity->getEntityTypeId());
    $controller->resetCache(array($entity->id()));
    return $controller->load($entity->id());
  }

  /**
   * Returns the entity_test hook invocation info.
   *
   * @return array
   *   An associative array of arbitrary hook data keyed by hook name.
   */
  protected function getHooksInfo() {
    $key = 'entity_test.hooks';
    $hooks = $this->state->get($key);
    $this->state->set($key, array());
    return $hooks;
  }