Verified Commit 6dd8b520 authored by Lee Rowlands's avatar Lee Rowlands
Browse files

Issue #3016038 by dpi, acbramley, jibran, dww, phenaproxima, jungle, Mingsong:...

Issue #3016038 by dpi, acbramley, jibran, dww, phenaproxima, jungle, Mingsong: Unrecognised entity operation passed to Menu Link Content throws exceptions

(cherry picked from commit 5920411f)
parent 704a5196
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -74,6 +74,9 @@ protected function checkAccess(EntityInterface $entity, $operation, AccountInter
      case 'delete':
        return AccessResult::allowedIfHasPermission($account, 'administer menu')
          ->andIf(AccessResult::allowedIf(!$entity->isNew())->addCacheableDependency($entity));

      default:
        return parent::checkAccess($entity, $operation, $account);
    }
  }

+54 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\menu_link_content\Unit;

use Drupal\Core\Access\AccessManagerInterface;
use Drupal\Core\Access\AccessResultInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\menu_link_content\MenuLinkContentAccessControlHandler;
use Drupal\Tests\UnitTestCase;

/**
 * Tests menu link content entity access.
 *
 * @coversDefaultClass \Drupal\menu_link_content\MenuLinkContentAccessControlHandler
 * @group menu_link_content
 */
class MenuLinkContentEntityAccessTest extends UnitTestCase {

  /**
   * Tests an operation not implemented by the access control handler.
   *
   * @covers ::checkAccess
   */
  public function testUnrecognizedOperation() {
    $entityType = $this->createMock(EntityTypeInterface::class);
    $accessManager = $this->createMock(AccessManagerInterface::class);
    $moduleHandler = $this->createMock(ModuleHandlerInterface::class);
    $moduleHandler->expects($this->any())
      ->method('invokeAll')
      ->willReturn([]);

    $language = $this->createMock(LanguageInterface::class);
    $language->expects($this->any())
      ->method('getId')
      ->will($this->returnValue('de'));

    $entity = $this->createMock(ContentEntityInterface::class);
    $entity->expects($this->any())
      ->method('language')
      ->willReturn($language);

    $account = $this->createMock(AccountInterface::class);

    $accessControl = new MenuLinkContentAccessControlHandler($entityType, $accessManager);
    $accessControl->setModuleHandler($moduleHandler);
    $access = $accessControl->access($entity, 'not-an-op', $account, TRUE);
    $this->assertInstanceOf(AccessResultInterface::class, $access);
  }

}