BigPipeStrategyTest.php 6.85 KB
Newer Older
1 2 3 4 5
<?php

namespace Drupal\Tests\big_pipe\Unit\Render\Placeholder;

use Drupal\big_pipe\Render\Placeholder\BigPipeStrategy;
6
use Drupal\big_pipe\Tests\BigPipePlaceholderTestCases;
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\SessionConfigurationInterface;
use Drupal\Tests\UnitTestCase;
use Prophecy\Argument;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\Route;

/**
 * @coversDefaultClass \Drupal\big_pipe\Render\Placeholder\BigPipeStrategy
 * @group big_pipe
 */
class BigPipeStrategyTest extends UnitTestCase {

  /**
   * @covers ::processPlaceholders
   *
   * @dataProvider placeholdersProvider
   */
26
  public function testProcessPlaceholders(array $placeholders, $method, $route_match_has_no_big_pipe_option, $request_has_session, $request_has_big_pipe_nojs_cookie, array $expected_big_pipe_placeholders) {
27
    $request = new Request();
28
    $request->setMethod($method);
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
    if ($request_has_big_pipe_nojs_cookie) {
      $request->cookies->set(BigPipeStrategy::NOJS_COOKIE, 1);
    }
    $request_stack = $this->prophesize(RequestStack::class);
    $request_stack->getCurrentRequest()
      ->willReturn($request);

    $session_configuration = $this->prophesize(SessionConfigurationInterface::class);
    $session_configuration->hasSession(Argument::type(Request::class))
      ->willReturn($request_has_session);

    $route = $this->prophesize(Route::class);
    $route->getOption('_no_big_pipe')
      ->willReturn($route_match_has_no_big_pipe_option);
    $route_match = $this->prophesize(RouteMatchInterface::class);
    $route_match->getRouteObject()
      ->willReturn($route);

    $big_pipe_strategy = new BigPipeStrategy($session_configuration->reveal(), $request_stack->reveal(), $route_match->reveal());
    $processed_placeholders = $big_pipe_strategy->processPlaceholders($placeholders);

50
    if ($request->isMethodSafe() && !$route_match_has_no_big_pipe_option && $request_has_session) {
51 52 53 54 55 56 57 58 59 60 61 62 63 64
      $this->assertSameSize($expected_big_pipe_placeholders, $processed_placeholders, 'BigPipe is able to deliver all placeholders.');
      foreach (array_keys($placeholders) as $placeholder) {
        $this->assertSame($expected_big_pipe_placeholders[$placeholder], $processed_placeholders[$placeholder], "Verifying how BigPipeStrategy handles the placeholder '$placeholder'");
      }
    }
    else {
      $this->assertSame(0, count($processed_placeholders));
    }
  }

  /**
   * @see \Drupal\big_pipe\Tests\BigPipePlaceholderTestCases
   */
  public function placeholdersProvider() {
65
    $cases = BigPipePlaceholderTestCases::cases();
66 67 68 69 70 71 72 73 74

    // Generate $placeholders variable as expected by
    // \Drupal\Core\Render\Placeholder\PlaceholderStrategyInterface::processPlaceholders().
    $placeholders = [
      $cases['html']->placeholder                             => $cases['html']->placeholderRenderArray,
      $cases['html_attribute_value']->placeholder             => $cases['html_attribute_value']->placeholderRenderArray,
      $cases['html_attribute_value_subset']->placeholder      => $cases['html_attribute_value_subset']->placeholderRenderArray,
      $cases['edge_case__invalid_html']->placeholder          => $cases['edge_case__invalid_html']->placeholderRenderArray,
      $cases['edge_case__html_non_lazy_builder']->placeholder => $cases['edge_case__html_non_lazy_builder']->placeholderRenderArray,
75 76
      $cases['exception__lazy_builder']->placeholder          => $cases['exception__lazy_builder']->placeholderRenderArray,
      $cases['exception__embedded_response']->placeholder     => $cases['exception__embedded_response']->placeholderRenderArray,
77 78 79
    ];

    return [
80 81 82 83 84 85 86
      '_no_big_pipe absent, no session, no-JS cookie absent' => [$placeholders, 'GET', FALSE, FALSE, FALSE, []],
      '_no_big_pipe absent, no session, no-JS cookie present' => [$placeholders, 'GET', FALSE, FALSE, TRUE, []],
      '_no_big_pipe present, no session, no-JS cookie absent' => [$placeholders, 'GET', TRUE, FALSE, FALSE, []],
      '_no_big_pipe present, no session, no-JS cookie present' => [$placeholders, 'GET', TRUE, FALSE, TRUE, []],
      '_no_big_pipe present, session, no-JS cookie absent' => [$placeholders, 'GET', TRUE, TRUE, FALSE, []],
      '_no_big_pipe present, session, no-JS cookie present' => [$placeholders, 'GET', TRUE, TRUE, TRUE, []],
      '_no_big_pipe absent, session, no-JS cookie absent: (JS-powered) BigPipe placeholder used for HTML placeholders' => [$placeholders, 'GET', FALSE, TRUE, FALSE, [
87 88 89 90 91
        $cases['html']->placeholder                             => $cases['html']->bigPipePlaceholderRenderArray,
        $cases['html_attribute_value']->placeholder             => $cases['html_attribute_value']->bigPipeNoJsPlaceholderRenderArray,
        $cases['html_attribute_value_subset']->placeholder      => $cases['html_attribute_value_subset']->bigPipeNoJsPlaceholderRenderArray,
        $cases['edge_case__invalid_html']->placeholder          => $cases['edge_case__invalid_html']->bigPipeNoJsPlaceholderRenderArray,
        $cases['edge_case__html_non_lazy_builder']->placeholder => $cases['edge_case__html_non_lazy_builder']->bigPipePlaceholderRenderArray,
92 93
        $cases['exception__lazy_builder']->placeholder          => $cases['exception__lazy_builder']->bigPipePlaceholderRenderArray,
        $cases['exception__embedded_response']->placeholder     => $cases['exception__embedded_response']->bigPipePlaceholderRenderArray,
94
      ]],
95 96
      '_no_big_pipe absent, session, no-JS cookie absent: (JS-powered) BigPipe placeholder used for HTML placeholders — but unsafe method' => [$placeholders, 'POST', FALSE, TRUE, FALSE, []],
      '_no_big_pipe absent, session, no-JS cookie present: no-JS BigPipe placeholder used for HTML placeholders' => [$placeholders, 'GET', FALSE, TRUE, TRUE, [
97 98 99 100 101
        $cases['html']->placeholder                             => $cases['html']->bigPipeNoJsPlaceholderRenderArray,
        $cases['html_attribute_value']->placeholder             => $cases['html_attribute_value']->bigPipeNoJsPlaceholderRenderArray,
        $cases['html_attribute_value_subset']->placeholder      => $cases['html_attribute_value_subset']->bigPipeNoJsPlaceholderRenderArray,
        $cases['edge_case__invalid_html']->placeholder          => $cases['edge_case__invalid_html']->bigPipeNoJsPlaceholderRenderArray,
        $cases['edge_case__html_non_lazy_builder']->placeholder => $cases['edge_case__html_non_lazy_builder']->bigPipeNoJsPlaceholderRenderArray,
102 103
        $cases['exception__lazy_builder']->placeholder          => $cases['exception__lazy_builder']->bigPipeNoJsPlaceholderRenderArray,
        $cases['exception__embedded_response']->placeholder     => $cases['exception__embedded_response']->bigPipeNoJsPlaceholderRenderArray,
104
      ]],
105
      '_no_big_pipe absent, session, no-JS cookie present: no-JS BigPipe placeholder used for HTML placeholders — but unsafe method' => [$placeholders, 'POST', FALSE, TRUE, TRUE, []],
106 107 108 109
    ];
  }

}