DynamicMenuLinkMock.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace Drupal\Tests\Core\Menu;
  3. use Drupal\Core\Session\AccountInterface;
  4. /**
  5. * Defines a mock implementation of a dynamic menu link used in tests only.
  6. *
  7. * Has a dynamic route and title. This is rather contrived, but there are valid
  8. * use cases.
  9. *
  10. * @see \Drupal\user\Plugin\Menu\LoginLogoutMenuLink
  11. */
  12. class DynamicMenuLinkMock extends MenuLinkMock {
  13. /**
  14. * The current user.
  15. *
  16. * @var \Drupal\Core\Session\AccountInterface
  17. */
  18. protected $currentUser;
  19. /**
  20. * Sets the current user.
  21. *
  22. * Allows the menu link to return the right title and route.
  23. *
  24. * @param \Drupal\Core\Session\AccountInterface $current_user
  25. * The current user.
  26. *
  27. * @return $this
  28. */
  29. public function setCurrentUser(AccountInterface $current_user) {
  30. $this->currentUser = $current_user;
  31. return $this;
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function getTitle() {
  37. if ($this->currentUser->isAuthenticated()) {
  38. return 'Log out';
  39. }
  40. else {
  41. return 'Log in';
  42. }
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function getRouteName() {
  48. if ($this->currentUser->isAuthenticated()) {
  49. return 'user.logout';
  50. }
  51. else {
  52. return 'user.login';
  53. }
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function getCacheContexts() {
  59. return ['user.roles:authenticated'];
  60. }
  61. }