class DynamicMenuLinkMock

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Menu/DynamicMenuLinkMock.php \Drupal\Tests\Core\Menu\DynamicMenuLinkMock
  2. 8.9.x core/tests/Drupal/Tests/Core/Menu/DynamicMenuLinkMock.php \Drupal\Tests\Core\Menu\DynamicMenuLinkMock
  3. 11.x core/tests/Drupal/Tests/Core/Menu/DynamicMenuLinkMock.php \Drupal\Tests\Core\Menu\DynamicMenuLinkMock

Defines a mock implementation of a dynamic menu link used in tests only.

Has a dynamic route and title. This is rather contrived, but there are valid use cases.

Hierarchy

Expanded class hierarchy of DynamicMenuLinkMock

See also

\Drupal\user\Plugin\Menu\LoginLogoutMenuLink

File

core/tests/Drupal/Tests/Core/Menu/DynamicMenuLinkMock.php, line 17

Namespace

Drupal\Tests\Core\Menu
View source
class DynamicMenuLinkMock extends MenuLinkMock {
    
    /**
     * The current user.
     *
     * @var \Drupal\Core\Session\AccountInterface
     */
    protected $currentUser;
    
    /**
     * Sets the current user.
     *
     * Allows the menu link to return the right title and route.
     *
     * @param \Drupal\Core\Session\AccountInterface $current_user
     *   The current user.
     *
     * @return $this
     */
    public function setCurrentUser(AccountInterface $current_user) {
        $this->currentUser = $current_user;
        return $this;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getTitle() {
        if ($this->currentUser
            ->isAuthenticated()) {
            return 'Log out';
        }
        else {
            return 'Log in';
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public function getRouteName() {
        if ($this->currentUser
            ->isAuthenticated()) {
            return 'user.logout';
        }
        else {
            return 'user.login';
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public function getCacheContexts() {
        return [
            'user.roles:authenticated',
        ];
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
DerivativeInspectionInterface::getBaseId public function Gets the base_plugin_id of the plugin instance. 1
DerivativeInspectionInterface::getDerivativeId public function Gets the derivative_id of the plugin instance. 1
DynamicMenuLinkMock::$currentUser protected property The current user.
DynamicMenuLinkMock::getCacheContexts public function Overrides MenuLinkMock::getCacheContexts
DynamicMenuLinkMock::getRouteName public function Overrides MenuLinkBase::getRouteName
DynamicMenuLinkMock::getTitle public function Overrides MenuLinkMock::getTitle
DynamicMenuLinkMock::setCurrentUser public function Sets the current user.
MenuLinkBase::$overrideAllowed protected property The list of definition values where an override is allowed. 3
MenuLinkBase::deleteLink public function Overrides MenuLinkInterface::deleteLink 2
MenuLinkBase::getDeleteRoute public function Overrides MenuLinkInterface::getDeleteRoute 1
MenuLinkBase::getEditRoute public function Overrides MenuLinkInterface::getEditRoute 1
MenuLinkBase::getFormClass public function Overrides MenuLinkInterface::getFormClass
MenuLinkBase::getMenuName public function Overrides MenuLinkInterface::getMenuName
MenuLinkBase::getMetaData public function Overrides MenuLinkInterface::getMetaData
MenuLinkBase::getOperations public function Overrides MenuLinkInterface::getOperations 1
MenuLinkBase::getOptions public function Overrides MenuLinkInterface::getOptions
MenuLinkBase::getParent public function Overrides MenuLinkInterface::getParent
MenuLinkBase::getProvider public function Overrides MenuLinkInterface::getProvider
MenuLinkBase::getResetRoute public function Overrides MenuLinkInterface::getResetRoute 1
MenuLinkBase::getRouteParameters public function Overrides MenuLinkInterface::getRouteParameters
MenuLinkBase::getTranslateRoute public function Overrides MenuLinkInterface::getTranslateRoute 1
MenuLinkBase::getUrlObject public function Overrides MenuLinkInterface::getUrlObject
MenuLinkBase::getWeight public function Overrides MenuLinkInterface::getWeight
MenuLinkBase::isDeletable public function Overrides MenuLinkInterface::isDeletable 2
MenuLinkBase::isEnabled public function Overrides MenuLinkInterface::isEnabled
MenuLinkBase::isExpanded public function Overrides MenuLinkInterface::isExpanded 1
MenuLinkBase::isResettable public function Overrides MenuLinkInterface::isResettable 1
MenuLinkBase::isTranslatable public function Overrides MenuLinkInterface::isTranslatable 1
MenuLinkMock::$defaults protected static property
MenuLinkMock::create public static function Create an instance from a definition with at least id, title, route_name.
MenuLinkMock::getCacheMaxAge public function Overrides MenuLinkBase::getCacheMaxAge
MenuLinkMock::getCacheTags public function Overrides MenuLinkBase::getCacheTags
MenuLinkMock::getDescription public function Overrides MenuLinkInterface::getDescription
MenuLinkMock::updateLink public function Overrides MenuLinkInterface::updateLink
PluginInspectionInterface::getPluginDefinition public function Gets the definition of the plugin implementation. 6
PluginInspectionInterface::getPluginId public function Gets the plugin ID of the plugin instance. 2

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.