You are here

class MockAliasManager in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Routing/MockAliasManager.php \Drupal\system\Tests\Routing\MockAliasManager

An easily configurable mock alias manager.

Hierarchy

Expanded class hierarchy of MockAliasManager

1 file declares its use of MockAliasManager
RequestPathTest.php in core/modules/system/src/Tests/Plugin/Condition/RequestPathTest.php
Contains \Drupal\system\Tests\Plugin\Condition\RequestPathTest.

File

core/modules/system/src/Tests/Routing/MockAliasManager.php, line 15
Contains \Drupal\system\Tests\Routing\MockAliasManager.

Namespace

Drupal\system\Tests\Routing
View source
class MockAliasManager implements AliasManagerInterface {

  /**
   * Array of mocked aliases. Keys are system paths, followed by language.
   *
   * @var array
   */
  protected $aliases = array();

  /**
   * Array of mocked aliases. Keys are aliases, followed by language.
   *
   * @var array
   */
  protected $systemPaths = array();

  /**
   * An index of aliases that have been requested.
   *
   * @var array
   */
  protected $lookedUp = array();

  /**
   * The language to assume a path alias is for if not specified.
   *
   * @var string
   */
  public $defaultLanguage = 'en';

  /**
   * Adds an alias to the in-memory alias table for this object.
   *
   * @param type $path
   *   The system path of the alias.
   * @param type $alias
   *   The alias of the system path.
   * @param type $path_language
   *   The language of this alias.
   */
  public function addAlias($path, $alias, $path_language = NULL) {
    $language = $path_language ?: $this->defaultLanguage;
    if ($path[0] !== '/') {
      throw new \InvalidArgumentException('The path needs to start with a slash.');
    }
    if ($alias[0] !== '/') {
      throw new \InvalidArgumentException('The alias needs to start with a slash.');
    }
    $this->aliases[$path][$language] = $alias;
    $this->systemPaths[$alias][$language] = $path;
  }

  /**
   * {@inheritdoc}
   */
  public function getPathByAlias($alias, $langcode = NULL) {
    $langcode = $langcode ?: $this->defaultLanguage;
    return $this->systemPaths[$alias][$langcode];
  }

  /**
   * {@inheritdoc}
   * @param $path
   * @param null $langcode
   * @return
   */
  public function getAliasByPath($path, $langcode = NULL) {
    $langcode = $langcode ?: $this->defaultLanguage;
    $this->lookedUp[$path] = 1;
    return $this->aliases[$path][$langcode];
  }

  /**
   * {@inheritdoc}
   */
  public function cacheClear($source = NULL) {

    // Not needed.
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MockAliasManager::$aliases protected property Array of mocked aliases. Keys are system paths, followed by language.
MockAliasManager::$defaultLanguage public property The language to assume a path alias is for if not specified.
MockAliasManager::$lookedUp protected property An index of aliases that have been requested.
MockAliasManager::$systemPaths protected property Array of mocked aliases. Keys are aliases, followed by language.
MockAliasManager::addAlias public function Adds an alias to the in-memory alias table for this object.
MockAliasManager::cacheClear public function Clear internal caches in alias manager. Overrides AliasManagerInterface::cacheClear
MockAliasManager::getAliasByPath public function Overrides AliasManagerInterface::getAliasByPath
MockAliasManager::getPathByAlias public function Given the alias, return the path it represents. Overrides AliasManagerInterface::getPathByAlias