PathAliasTestTrait.php 3.41 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php

namespace Drupal\Tests\Traits\Core;

use Drupal\Core\Language\LanguageInterface;

/**
 * Provides methods to create and assert path_alias entities.
 *
 * This trait is meant to be used only by test classes.
 */
trait PathAliasTestTrait {

  /**
   * Creates a new path alias.
   *
   * @param string $path
   *   The system path.
   * @param string $alias
   *   The alias for the system path.
   * @param string $langcode
   *   (optional) A language code for the path alias. Defaults to
   *   \Drupal\Core\Language\LanguageInterface::LANGCODE_NOT_SPECIFIED.
   *
25
   * @return \Drupal\path_alias\PathAliasInterface
26
27
28
   *   A path alias entity.
   */
  protected function createPathAlias($path, $alias, $langcode = LanguageInterface::LANGCODE_NOT_SPECIFIED) {
29
    /** @var \Drupal\path_alias\PathAliasInterface $path_alias */
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
    $path_alias = \Drupal::entityTypeManager()->getStorage('path_alias')->create([
      'path' => $path,
      'alias' => $alias,
      'langcode' => $langcode,
    ]);
    $path_alias->save();

    return $path_alias;
  }

  /**
   * Gets the first result from a 'load by properties' storage call.
   *
   * @param array $conditions
   *   An array of query conditions.
   *
46
   * @return \Drupal\path_alias\PathAliasInterface|null
47
48
49
50
   *   A path alias entity or NULL.
   */
  protected function loadPathAliasByConditions($conditions) {
    $storage = \Drupal::entityTypeManager()->getStorage('path_alias');
51
    $query = $storage->getQuery()->accessCheck(FALSE);
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
    foreach ($conditions as $field => $value) {
      $query->condition($field, $value);
    }
    $entities = $storage->loadMultiple($query->execute());

    return $entities ? reset($entities) : NULL;
  }

  /**
   * Asserts that a path alias exists in the storage.
   *
   * @param string $alias
   *   The path alias.
   * @param string|null $langcode
   *   (optional) The language code of the path alias.
   * @param string|null $path
   *   (optional) The system path of the path alias.
   * @param string|null $message
   *   (optional) A message to display with the assertion.
   */
72
  protected function assertPathAliasExists($alias, $langcode = NULL, $path = NULL, $message = '') {
73
74
75
76
    $query = \Drupal::entityTypeManager()
      ->getStorage('path_alias')
      ->getQuery()
      ->accessCheck(FALSE);
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
    $query->condition('alias', $alias, '=');
    if ($langcode) {
      $query->condition('langcode', $langcode, '=');
    }
    if ($path) {
      $query->condition('path', $path, '=');
    }
    $query->count();

    $this->assertTrue((bool) $query->execute(), $message);
  }

  /**
   * Asserts that a path alias does not exist in the storage.
   *
   * @param string $alias
   *   The path alias.
   * @param string|null $langcode
   *   (optional) The language code of the path alias.
   * @param string|null $path
   *   (optional) The system path of the path alias.
   * @param string|null $message
   *   (optional) A message to display with the assertion.
   */
101
  protected function assertPathAliasNotExists($alias, $langcode = NULL, $path = NULL, $message = '') {
102
103
104
105
    $query = \Drupal::entityTypeManager()
      ->getStorage('path_alias')
      ->getQuery()
      ->accessCheck(FALSE);
106
107
108
109
110
111
112
113
114
115
116
117
118
    $query->condition('alias', $alias, '=');
    if ($langcode) {
      $query->condition('langcode', $langcode, '=');
    }
    if ($path) {
      $query->condition('path', $path, '=');
    }
    $query->count();

    $this->assertFalse((bool) $query->execute(), $message);
  }

}