PathAliasTestTrait.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace Drupal\Tests\Traits\Core;
  3. use Drupal\Core\Language\LanguageInterface;
  4. /**
  5. * Provides methods to create and assert path_alias entities.
  6. *
  7. * This trait is meant to be used only by test classes.
  8. */
  9. trait PathAliasTestTrait {
  10. /**
  11. * Creates a new path alias.
  12. *
  13. * @param string $path
  14. * The system path.
  15. * @param string $alias
  16. * The alias for the system path.
  17. * @param string $langcode
  18. * (optional) A language code for the path alias. Defaults to
  19. * \Drupal\Core\Language\LanguageInterface::LANGCODE_NOT_SPECIFIED.
  20. *
  21. * @return \Drupal\path_alias\PathAliasInterface
  22. * A path alias entity.
  23. */
  24. protected function createPathAlias($path, $alias, $langcode = LanguageInterface::LANGCODE_NOT_SPECIFIED) {
  25. /** @var \Drupal\path_alias\PathAliasInterface $path_alias */
  26. $path_alias = \Drupal::entityTypeManager()->getStorage('path_alias')->create([
  27. 'path' => $path,
  28. 'alias' => $alias,
  29. 'langcode' => $langcode,
  30. ]);
  31. $path_alias->save();
  32. return $path_alias;
  33. }
  34. /**
  35. * Gets the first result from a 'load by properties' storage call.
  36. *
  37. * @param array $conditions
  38. * An array of query conditions.
  39. *
  40. * @return \Drupal\path_alias\PathAliasInterface|null
  41. * A path alias entity or NULL.
  42. */
  43. protected function loadPathAliasByConditions($conditions) {
  44. $storage = \Drupal::entityTypeManager()->getStorage('path_alias');
  45. $query = $storage->getQuery();
  46. foreach ($conditions as $field => $value) {
  47. $query->condition($field, $value);
  48. }
  49. $entities = $storage->loadMultiple($query->execute());
  50. return $entities ? reset($entities) : NULL;
  51. }
  52. /**
  53. * Asserts that a path alias exists in the storage.
  54. *
  55. * @param string $alias
  56. * The path alias.
  57. * @param string|null $langcode
  58. * (optional) The language code of the path alias.
  59. * @param string|null $path
  60. * (optional) The system path of the path alias.
  61. * @param string|null $message
  62. * (optional) A message to display with the assertion.
  63. */
  64. protected function assertPathAliasExists($alias, $langcode = NULL, $path = NULL, $message = NULL) {
  65. $query = \Drupal::entityTypeManager()->getStorage('path_alias')->getQuery();
  66. $query->condition('alias', $alias, '=');
  67. if ($langcode) {
  68. $query->condition('langcode', $langcode, '=');
  69. }
  70. if ($path) {
  71. $query->condition('path', $path, '=');
  72. }
  73. $query->count();
  74. $this->assertTrue((bool) $query->execute(), $message);
  75. }
  76. /**
  77. * Asserts that a path alias does not exist in the storage.
  78. *
  79. * @param string $alias
  80. * The path alias.
  81. * @param string|null $langcode
  82. * (optional) The language code of the path alias.
  83. * @param string|null $path
  84. * (optional) The system path of the path alias.
  85. * @param string|null $message
  86. * (optional) A message to display with the assertion.
  87. */
  88. protected function assertPathAliasNotExists($alias, $langcode = NULL, $path = NULL, $message = NULL) {
  89. $query = \Drupal::entityTypeManager()->getStorage('path_alias')->getQuery();
  90. $query->condition('alias', $alias, '=');
  91. if ($langcode) {
  92. $query->condition('langcode', $langcode, '=');
  93. }
  94. if ($path) {
  95. $query->condition('path', $path, '=');
  96. }
  97. $query->count();
  98. $this->assertFalse((bool) $query->execute(), $message);
  99. }
  100. }