getRedirect example

$this->logger($this->revision->getEntityType()->getProvider())->info('@type: reverted %title revision %revision.', [
      '@type' => $this->revision->bundle(),
      '%title' => $revisionLabel,
      '%revision' => $revisionId,
    ]);

    $versionHistoryUrl = $this->revision->toUrl('version-history');
    if ($versionHistoryUrl->access($this->currentUser())) {
      $form_state->setRedirectUrl($versionHistoryUrl);
    }

    if (!$form_state->getRedirect()) {
      $canonicalUrl = $this->revision->toUrl();
      if ($canonicalUrl->access($this->currentUser())) {
        $form_state->setRedirectUrl($canonicalUrl);
      }
    }
  }

  /** * Prepares a revision to be reverted. * * @param \Drupal\Core\Entity\RevisionableInterface $revision * The revision to be reverted. * @param \Drupal\Core\Form\FormStateInterface $formState * The current state of the form. * * @return \Drupal\Core\Entity\RevisionableInterface * The new revision, the same type as passed to $revision. */
class FormStateTest extends UnitTestCase {

  /** * Tests the getRedirect() method. * * @covers ::getRedirect * * @dataProvider providerTestGetRedirect */
  public function testGetRedirect($form_state_additions$expected) {
    $form_state = (new FormState())->setFormState($form_state_additions);
    $redirect = $form_state->getRedirect();
    $this->assertEquals($expected$redirect);
  }

  /** * Provides test data for testing the getRedirect() method. * * @return array * Returns some test data. */
  public function providerTestGetRedirect() {
    $data = [];
    

  public function setRedirectUrl(Url $url) {
    $this->decoratedFormState->setRedirectUrl($url);

    return $this;
  }

  /** * {@inheritdoc} */
  public function getRedirect() {
    return $this->decoratedFormState->getRedirect();
  }

  /** * {@inheritdoc} */
  public static function hasAnyErrors() {
    return FormState::hasAnyErrors();
  }

  /** * {@inheritdoc} */
/** * @covers ::getRedirect * * @dataProvider providerGetRedirect * * @param bool $expected * Any valid value for \Drupal\Core\Form\FormStateInterface::getRedirect()'s * return value. */
  public function testGetRedirect($expected) {
    $this->decoratedFormState->getRedirect()
      ->willReturn($expected)
      ->shouldBeCalled();

    $this->assertSame($expected$this->formStateDecoratorBase->getRedirect());
  }

  /** * Provides data to self::testGetRedirect(). */
  public function providerGetRedirect() {
    return [
      [
->accessCheck(FALSE)
        ->allRevisions()
        ->condition($this->revision->getEntityType()->getKey('id')$this->revision->id())
        ->count()
        ->execute();
      $versionHistoryUrl = $this->revision->toUrl('version-history');
      if ($remainingRevisions && $versionHistoryUrl->access($this->currentUser())) {
        $form_state->setRedirectUrl($versionHistoryUrl);
      }
    }

    if (!$form_state->getRedirect()) {
      $canonicalUrl = $this->revision->toUrl();
      if ($canonicalUrl->access($this->currentUser())) {
        $form_state->setRedirectUrl($canonicalUrl);
      }
    }
  }

  /** * Returns the bundle label of an entity. * * @param \Drupal\Core\Entity\RevisionableInterface $entity * The entity. * * @return string|null * The bundle label. */
$kernel->boot();

        $container = $kernel->getContainer();

        // transform request to resolve seo urls and detect sales channel         $transformed = $container
            ->get(RequestTransformerInterface::class)
            ->transform($request);

        $redirect = $container
            ->get(CanonicalRedirectService::class)
            ->getRedirect($transformed);

        if ($redirect instanceof RedirectResponse) {
            $event = new BeforeSendRedirectResponseEvent($transformed$redirect);
            $container->get('event_dispatcher')->dispatch($event);

            return new HttpKernelResult($transformed$event->getResponse());
        }

        // check for http caching         $enabled = $container->hasParameter('shopware.http.cache.enabled')
            && $container->getParameter('shopware.http.cache.enabled');
        

      else {
        call_user_func_array($form_state->prepareCallback($callback)[&$form, &$form_state]);
      }
    }
  }

  /** * {@inheritdoc} */
  public function redirectForm(FormStateInterface $form_state) {
    $redirect = $form_state->getRedirect();

    // Allow using redirect responses directly if needed.     if ($redirect instanceof RedirectResponse) {
      return $redirect;
    }

    $url = NULL;
    // Check for a route-based redirection.     if ($redirect instanceof Url) {
      $url = $redirect->setAbsolute()->toString();
    }
    
final public const CONFIG_KEY = 'core.seo.redirectToCanonicalUrl';

    /** * @dataProvider requestDataProvider */
    public function testGetRedirect(Request $request, ?Response $response): void
    {
        $shouldRedirect = $response->getStatusCode() === Response::HTTP_MOVED_PERMANENTLY;
        $canonicalRedirectService = new CanonicalRedirectService($this->getSystemConfigService($shouldRedirect));

        /** @var RedirectResponse|null $actual */
        $actual = $canonicalRedirectService->getRedirect($request);

        if ($shouldRedirect) {
            static::assertNotNull($actual);
            static::assertInstanceOf(
                RedirectResponse::class,
                $actual
            );
            static::assertEquals(
                $request->attributes->get(SalesChannelRequest::ATTRIBUTE_CANONICAL_LINK),
                $actual->getTargetUrl()
            );
            
$c = new RequestDataCollector();

        $request = $this->createRequest();
        $request->attributes->set('_redirected', true);
        $request->cookies->add([
            'sf_redirect' => '{"method": "POST"}',
        ]);

        $c->collect($request$response = $this->createResponse());
        $c->lateCollect();

        $this->assertEquals('POST', $c->getRedirect()['method']);

        $cookie = $this->getCookieByName($response, 'sf_redirect');
        $this->assertNull($cookie->getValue());
    }

    public function testItCollectsTheSessionTraceProperly()
    {
        $collector = new RequestDataCollector();
        $request = $this->createRequest();

        // RequestDataCollectorTest doesn't implement SessionInterface or SessionBagInterface, therefore should do nothing.

  public function submitDelayDestination($form, FormStateInterface $form_state) {
    $request = $this->requestStack->getCurrentRequest();
    $destination = $request->query->get('destination');

    $redirect = $form_state->getRedirect();
    // If there is a destination, and redirects are not explicitly disabled, add     // the destination as a query string to the redirect and suppress it for the     // current request.     if (isset($destination) && $redirect !== FALSE) {
      // Create a valid redirect if one does not exist already.       if (!($redirect instanceof Url)) {
        $redirect = Url::createFromRequest($request);
      }

      // Add the current destination to the redirect unless one exists already.       $options = $redirect->getOptions();
      

        $collector = new RouterDataCollector();

        $request = Request::create('http://test.com/foo?bar=baz');
        $response = new RedirectResponse('http://test.com/redirect');

        $event = $this->createControllerEvent($request);

        $collector->onKernelController($event);
        $collector->collect($request$response);

        $this->assertTrue($collector->getRedirect());
        $this->assertEquals('http://test.com/redirect', $collector->getTargetUrl());
        $this->assertEquals('n/a', $collector->getTargetRoute());
    }

    public function testRouteNotRedirectResponse()
    {
        $collector = new RouterDataCollector();

        $request = Request::create('http://test.com/foo?bar=baz');
        $response = new Response('test');

        
Home | Imprint | This part of the site doesn't use cookies.