You are here

class SystemPageRedirectTest in Rules 8.3

@coversDefaultClass \Drupal\rules\Plugin\RulesAction\SystemPageRedirect @group RulesAction

Hierarchy

Expanded class hierarchy of SystemPageRedirectTest

File

tests/src/Unit/Integration/RulesAction/SystemPageRedirectTest.php, line 17

Namespace

Drupal\Tests\rules\Unit\Integration\RulesAction
View source
class SystemPageRedirectTest extends RulesIntegrationTestBase {

  /**
   * A mocked Rules logger.channel.rules_debug service.
   *
   * @var \Drupal\Core\Logger\LoggerChannelInterface|\Prophecy\Prophecy\ProphecyInterface
   */
  protected $logger;

  /**
   * The mocked request stack service.
   *
   * @var \Symfony\Component\HttpFoundation\RequestStack|\Prophecy\Prophecy\ProphecyInterface
   */
  protected $requestStack;

  /**
   * The mocked current path stack service.
   *
   * @var \Drupal\Core\Path\CurrentPathStack|\Prophecy\Prophecy\ProphecyInterface
   */
  protected $currentPathStack;

  /**
   * A mocked request.
   *
   * @var \Symfony\Component\HttpFoundation\Request|\Prophecy\Prophecy\ProphecyInterface
   */
  protected $currentRequest;

  /**
   * A mocked parameter bag.
   *
   * @var \Symfony\Component\HttpFoundation\ParameterBag|\Prophecy\Prophecy\ProphecyInterface
   */
  protected $parameterBag;

  /**
   * The action to be tested.
   *
   * @var \Drupal\rules\Plugin\RulesAction\SystemPageRedirect
   */
  protected $action;

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();

    // Mock the Rules debug logger service, make it return our mocked logger,
    // and register it in the container.
    $this->logger = $this
      ->prophesize(LoggerChannelInterface::class);
    $this->container
      ->set('logger.channel.rules_debug', $this->logger
      ->reveal());

    // Mock a parameter bag.
    $this->parameterBag = $this
      ->prophesize(ParameterBag::class);

    // Mock a request, and set our mocked parameter bag as it attributes
    // property.
    $this->currentRequest = $this
      ->prophesize(Request::class);
    $this->currentRequest->attributes = $this->parameterBag
      ->reveal();

    // Mock the request stack, make it return our mocked request when the
    // current request is requested, and register it in the container.
    $this->requestStack = $this
      ->prophesize(RequestStack::class);
    $this->requestStack
      ->getCurrentRequest()
      ->willReturn($this->currentRequest);
    $this->container
      ->set('request_stack', $this->requestStack
      ->reveal());

    // Mock the current path stack.
    $this->currentPathStack = $this
      ->prophesize(CurrentPathStack::class);
    $this->container
      ->set('path.current', $this->currentPathStack
      ->reveal());

    // Instantiate the redirect action.
    $this->action = $this->actionManager
      ->createInstance('rules_page_redirect');
  }

  /**
   * Tests redirection.
   *
   * @covers ::execute
   */
  public function testRedirect() {
    $this->currentPathStack
      ->getPath()
      ->willReturn('some/random/test/path');
    $this->action
      ->setContextValue('url', '/test/url');
    $this->action
      ->execute();
    $this->parameterBag
      ->set('_rules_redirect_action_url', '/test/url')
      ->shouldHaveBeenCalled();
  }

  /**
   * Tests unsuccessful redirection due to ongoing batch process.
   *
   * @covers ::execute
   */
  public function testRedirectBatch() {
    $this->currentPathStack
      ->getPath()
      ->willReturn('some/random/test/path');
    batch_set('Test batch!');
    $this->action
      ->setContextValue('url', '/test/url');
    $this->action
      ->execute();
    $this->logger
      ->warning('Skipped page redirect during batch processing.')
      ->shouldHaveBeenCalled();
  }

  /**
   * Tests unsuccessful redirection due to rules admin page location.
   *
   * @covers ::execute
   */
  public function testRedirectRulesAdminPage() {
    $this->currentPathStack
      ->getPath()
      ->willReturn('admin/config/workflow/rules');
    $this->action
      ->setContextValue('url', '/test/url');
    $this->action
      ->execute();
    $this->logger
      ->warning('Skipped page redirect on a rules admin page.')
      ->shouldHaveBeenCalled();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
RulesIntegrationTestBase::$actionManager protected property
RulesIntegrationTestBase::$cacheBackend protected property
RulesIntegrationTestBase::$classResolver protected property The class resolver mock for the typed data manager.
RulesIntegrationTestBase::$conditionManager protected property
RulesIntegrationTestBase::$container protected property The Drupal service container.
RulesIntegrationTestBase::$dataFetcher protected property The data fetcher service.
RulesIntegrationTestBase::$dataFilterManager protected property The data filter manager.
RulesIntegrationTestBase::$enabledModules protected property Array object keyed with module names and TRUE as value.
RulesIntegrationTestBase::$entityFieldManager protected property
RulesIntegrationTestBase::$entityTypeBundleInfo protected property
RulesIntegrationTestBase::$entityTypeManager protected property
RulesIntegrationTestBase::$messenger protected property The messenger service.
RulesIntegrationTestBase::$moduleHandler protected property
RulesIntegrationTestBase::$namespaces protected property All setup'ed namespaces.
RulesIntegrationTestBase::$placeholderResolver protected property The placeholder resolver service.
RulesIntegrationTestBase::$rulesDataProcessorManager protected property
RulesIntegrationTestBase::$rulesExpressionManager protected property
RulesIntegrationTestBase::$typedDataManager protected property
RulesIntegrationTestBase::constructModulePath protected function Determines the path to a module's class files.
RulesIntegrationTestBase::enableModule protected function Fakes the enabling of a module and adds its namespace for plugin loading.
RulesIntegrationTestBase::getTypedData protected function Returns a typed data object.
RulesIntegrationTestBase::prophesizeEntity protected function Helper method to mock irrelevant cache methods on entities.
SystemPageRedirectTest::$action protected property The action to be tested.
SystemPageRedirectTest::$currentPathStack protected property The mocked current path stack service.
SystemPageRedirectTest::$currentRequest protected property A mocked request.
SystemPageRedirectTest::$logger protected property A mocked Rules logger.channel.rules_debug service. Overrides RulesIntegrationTestBase::$logger
SystemPageRedirectTest::$parameterBag protected property A mocked parameter bag.
SystemPageRedirectTest::$requestStack protected property The mocked request stack service.
SystemPageRedirectTest::setUp protected function Overrides RulesIntegrationTestBase::setUp
SystemPageRedirectTest::testRedirect public function Tests redirection.
SystemPageRedirectTest::testRedirectBatch public function Tests unsuccessful redirection due to ongoing batch process.
SystemPageRedirectTest::testRedirectRulesAdminPage public function Tests unsuccessful redirection due to rules admin page location.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.