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