3 namespace Drupal\system\Tests\Routing;
5 use Drupal\Core\Path\AliasManagerInterface;
8 * An easily configurable mock alias manager.
10 class MockAliasManager implements AliasManagerInterface {
13 * Array of mocked aliases. Keys are system paths, followed by language.
17 protected $aliases = [];
20 * Array of mocked aliases. Keys are aliases, followed by language.
24 protected $systemPaths = [];
27 * An index of aliases that have been requested.
31 protected $lookedUp = [];
34 * The language to assume a path alias is for if not specified.
38 public $defaultLanguage = 'en';
41 * Adds an alias to the in-memory alias table for this object.
44 * The system path of the alias.
45 * @param string $alias
46 * The alias of the system path.
47 * @param string $path_language
48 * The language of this alias.
50 public function addAlias($path, $alias, $path_language = NULL) {
51 $language = $path_language ?: $this->defaultLanguage;
53 if ($path[0] !== '/') {
54 throw new \InvalidArgumentException('The path needs to start with a slash.');
56 if ($alias[0] !== '/') {
57 throw new \InvalidArgumentException('The alias needs to start with a slash.');
60 $this->aliases[$path][$language] = $alias;
61 $this->systemPaths[$alias][$language] = $path;
67 public function getPathByAlias($alias, $langcode = NULL) {
68 $langcode = $langcode ?: $this->defaultLanguage;
69 return $this->systemPaths[$alias][$langcode];
75 * @param null $langcode
78 public function getAliasByPath($path, $langcode = NULL) {
79 if ($path[0] !== '/') {
80 throw new \InvalidArgumentException(sprintf('Source path %s has to start with a slash.', $path));
83 $langcode = $langcode ?: $this->defaultLanguage;
84 $this->lookedUp[$path] = 1;
85 return $this->aliases[$path][$langcode];
91 public function cacheClear($source = NULL) {