improvements-2.x-dev/tests/src/Functional/ImprovementsTest.php

tests/src/Functional/ImprovementsTest.php
<?php

namespace Drupal\Tests\improvements\Functional;

use Drupal\Core\Routing\RouteMatch;
use Drupal\Core\Url;
use Drupal\improvements\Cache\CookiesHashCacheContext;
use Drupal\improvements\Cache\EntityFieldCacheContext;
use Drupal\improvements\Cache\EntityTypeCacheContext;
use Drupal\improvements\Cache\IsEntityPageCacheContext;
use Drupal\improvements_test\Entity\SecondConfigEntity;
use Drupal\improvements_test\Entity\TestConfigEntity;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\improvements\Traits\ImprovementsTestTrait;
use Drupal\Tests\node\Traits\NodeCreationTrait;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\Route;

class ImprovementsTest extends BrowserTestBase {

  use ImprovementsTestTrait;
  use NodeCreationTrait;

  /**
   * {@inheritDoc}
   */
  protected $defaultTheme = 'stark';

  /**
   * {@inheritDoc}
   */
  protected static $modules = ['improvements'];

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

  /**
   * Test create link with attribute href="#".
   *
   * @covers \Drupal\improvements\ImprovementsTrustedCallbacks::linkPostRender()
   */
  public function testLinkWithOnlySharpSymbol(): void {
    $renderer = $this->container->get('renderer');
    $build = [
      '#type' => 'link',
      '#url' => Url::fromRoute('<none>', [], ['fragment' => '<none>']),
      '#title' => 'Test',
    ];
    $html = (string)$renderer->renderRoot($build);
    $this->assertSame('<a href="#">' . $build['#title'] . '</a>', $html);
  }

  /**
   * Test cookies_hash cache context.
   *
   * @covers \Drupal\improvements\Cache\CookiesHashCacheContext
   */
  public function testCacheContextCookiesHash(): void {
    $request_stack = new RequestStack();
    $request = Request::create(
      uri: '/',
      cookies: ['foo' => 'bar'],
    );
    $request_stack->push($request);
    $cache_context = new CookiesHashCacheContext($request_stack);
    $this->assertSame('64b41e5d', $cache_context->getContext());
    $this->assertSame('f4bb71a2', $cache_context->getContext('foo'));

    $request_stack = new RequestStack();
    $request = Request::create('/');
    $request_stack->push($request);
    $cache_context = new CookiesHashCacheContext($request_stack);
    $this->assertSame('', $cache_context->getContext());
    $this->assertSame('', $cache_context->getContext('foo'));
  }

  /**
   * Test entity_field cache context.
   *
   * @covers \Drupal\improvements\Cache\EntityFieldCacheContext
   */
  public function testCacheContextEntityField(): void {
    $this->installModule('node');

    $node = $this->createNode([
      'title' => 'Node for test ' . __FUNCTION__,
      'uid' => 1,
    ]);
    $node_route = new Route('/node/{node}');
    $node_route_match = new RouteMatch('entity.node.canonical', $node_route, ['node' => $node], ['node' => $node->id()]);
    $cache_context = new EntityFieldCacheContext($node_route_match);
    $this->assertSame((string)$node->get('uid')->target_id, $cache_context->getContext('uid'));
    $this->assertSame($node->label(), $cache_context->getContext('title'));
    $this->assertSame('', $cache_context->getContext('dummy'));

    $front_route = new Route('/');
    $front_route_match = new RouteMatch('<front>', $front_route);
    $cache_context = new EntityFieldCacheContext($front_route_match);
    $this->assertSame('', $cache_context->getContext('title'));
    $this->assertSame('', $cache_context->getContext('dummy'));
  }

  /**
   * Test entity_type cache context.
   *
   * @covers \Drupal\improvements\Cache\EntityTypeCacheContext
   */
  public function testCacheContextEntityType(): void {
    $this->installModule('node');

    $node = $this->createNode([
      'title' => 'Node for test ' . __FUNCTION__,
      'uid' => 1,
    ]);
    $route = new Route('/node/{node}');
    $route_match = new RouteMatch('entity.node.canonical', $route, ['node' => $node], ['node' => $node->id()]);
    $cache_context = new EntityTypeCacheContext($route_match);
    $this->assertSame('node', $cache_context->getContext());

    $route = new Route('/');
    $route_match = new RouteMatch('<front>', $route);
    $cache_context = new EntityTypeCacheContext($route_match);
    $this->assertSame('', $cache_context->getContext());
  }

  /**
   * Test is_entity_page cache context.
   *
   * @covers \Drupal\improvements\Cache\IsEntityPageCacheContext
   */
  public function testCacheContextIsEntityPage(): void {
    $this->installModule('node');

    $node = $this->createNode([
      'title' => 'Node for test ' . __FUNCTION__,
      'uid' => 1,
    ]);
    $route = new Route('/node/{node}');
    $route_match = new RouteMatch('entity.node.canonical', $route, ['node' => $node], ['node' => $node->id()]);
    $cache_context = new IsEntityPageCacheContext($route_match);
    $this->assertSame('1', $cache_context->getContext());
    $this->assertSame('1', $cache_context->getContext('node'));
    $this->assertSame('1', $cache_context->getContext('node,taxonomy_term'));
    $this->assertSame('0', $cache_context->getContext('taxonomy_term'));

    $route = new Route('/');
    $route_match = new RouteMatch('<front>', $route);
    $cache_context = new IsEntityPageCacheContext($route_match);
    $this->assertSame('0', $cache_context->getContext());
    $this->assertSame('0', $cache_context->getContext('node'));
    $this->assertSame('0', $cache_context->getContext('node,taxonomy_term'));
    $this->assertSame('0', $cache_context->getContext('taxonomy_term'));
  }

  /**
   * Test hook hook_page_ROUTE_NAME_result_alter().
   *
   * @covers \Drupal\improvements\ImprovementsEventSubscriber::onKernelView()
   */
  public function testHookPageResultAlter(): void {
    $this->installModule('improvements_test');
    $this->drupalGet('/improvements-test/altered-page-result');
    $this->dontSeeErrorMessage();
    $web_assert = $this->assertSession();
    $web_assert->pageTextContains('Altered result');
    $web_assert->pageTextNotContains('Markup from controller');
  }

  /**
   * Test filter plugin "nofollow_external_links_filter".
   *
   * @covers \Drupal\improvements\Plugin\Filter\NofollowExternalLinksFilter
   */
  public function testFilterPluginNofollowExternalLinks(): void {
    $this->installModule('filter');
    $filter = $this->createFilterFormat([
      'format' => 'test_nofollow_external_links',
      'name' => 'Test nofollow_external_links_filter',
      'filters' => [
        'nofollow_external_links_filter' => [
          'status' => 1,
        ],
      ],
    ]);
    $this->drupalLoginAsRoot();
    $this->drupalGet('/admin/config/content/formats/manage/' . $filter->id());
    $this->dontSeeErrorMessage();
    $web_assert = $this->assertSession();
    $web_assert->fieldExists('filters[nofollow_external_links_filter][status]');
    $web_assert->checkboxChecked('filters[nofollow_external_links_filter][status]');

    $result = check_markup('foo <a href="https://google.com">Google</a> bar', $filter->id());
    $this->assertSame('foo <a href="https://google.com" rel="nofollow">Google</a> bar', (string)$result);
  }

  /**
   * Test filter plugin "table_wrapper_filter".
   *
   * @covers \Drupal\improvements\Plugin\Filter\TableWrapperFilter
   */
  public function testFilterPluginTableWrapper(): void {
    $this->installModule('filter');
    $filter = $this->createFilterFormat([
      'format' => 'table_wrapper_filter',
      'name' => 'Test table_wrapper_filter',
      'filters' => [
        'table_wrapper_filter' => [
          'status' => 1,
        ],
      ],
    ]);
    $this->drupalLoginAsRoot();
    $this->drupalGet('/admin/config/content/formats/manage/' . $filter->id());
    $this->dontSeeErrorMessage();
    $web_assert = $this->assertSession();
    $web_assert->fieldExists('filters[table_wrapper_filter][status]');
    $web_assert->checkboxChecked('filters[table_wrapper_filter][status]');

    $result = check_markup('foo <table></table> bar', $filter->id());
    $this->assertSame('foo <div class="table-wrapper"><table></table></div> bar', (string)$result);
  }

  /**
   * Test access check by uuid.
   *
   * @covers \Drupal\improvements\Access\EntityUuidAccessCheck
   */
  public function testAccessCheckEntityUuid(): void {
    $this->installModule('improvements_test', 'node');
    $web_assert = $this->assertSession();
    $node = $this->createNode(['title' => 'Node for test ' . __FUNCTION__]);

    $this->drupalGet('/improvements-test/entity-uuid-access-check/' . $node->id());
    $web_assert->statusCodeEquals(403);

    $this->drupalGet('/improvements-test/entity-uuid-access-check/' . $node->id(), ['query' => ['uuid' => '123']]);
    $web_assert->statusCodeEquals(403);

    $this->drupalGet('/improvements-test/entity-uuid-access-check/' . $node->id(), ['query' => ['uuid' => 'true']]);
    $web_assert->statusCodeEquals(403);

    $this->drupalGet('/improvements-test/entity-uuid-access-check/' . $node->id(), ['query' => ['uuid' => '1']]);
    $web_assert->statusCodeEquals(403);

    $this->drupalGet('/improvements-test/entity-uuid-access-check/' . $node->id(), ['query' => ['uuid' => '0']]);
    $web_assert->statusCodeEquals(403);

    $this->drupalGet('/improvements-test/entity-uuid-access-check/' . $node->id(), ['query' => ['uuid' => $node->uuid()]]);
    $web_assert->statusCodeEquals(200);
  }

  /**
   * Test token [current-page:url-with-query]
   */
  public function testTokenCurrentPageUrlWithQuery(): void {
    $this->installModule('token');
    $token_service = $this->container->get('token');

    $request = Request::create('/admin', 'GET', ['foo' => 'bar']);
    $request_stack = $this->container->get('request_stack');
    $request_stack->push($request);

    $this->assertStringContainsString('/admin?foo=bar', $token_service->replace('[current-page:url-with-query]'));
    $this->assertSame('/admin?foo=bar', $token_service->replace('[current-page:url-with-query:relative]'));
  }

  /**
   * Test DefaultDraggableEntityListBuilder.
   *
   * @covers \Drupal\improvements\DefaultDraggableEntityListBuilder
   */
  public function testDefaultDraggableEntityListBuilder(): void {
    $this->installModule('improvements_test');
    $web_assert = $this->assertSession();
    $this->drupalLoginAsRoot();

    $entity1 = TestConfigEntity::create(['id' => 'ent1', 'label' => 'Test entity 1 for ' . __FUNCTION__, 'weight' => 1]);
    $entity1->save();
    $entity2 = TestConfigEntity::create(['id' => 'ent2', 'label' => 'Test entity 2 for ' . __FUNCTION__, 'weight' => 2]);
    $entity2->save();

    $this->drupalGet(Url::fromRoute('entity.test_config_entity.collection'));
    $this->dontSeeErrorMessage();
    $web_assert->pageTextContains($entity1->label());
    $web_assert->pageTextContains($entity2->label());
    $web_assert->fieldValueEquals('entities[' . $entity1->id() . '][weight]', $entity1->get('weight'));
    $web_assert->fieldValueEquals('entities[' . $entity2->id() . '][weight]', $entity2->get('weight'));

    $this->submitForm([
      'entities[' . $entity1->id() . '][weight]' => 2,
      'entities[' . $entity2->id() . '][weight]' => 1,
    ], 'Save');
    $this->dontSeeErrorMessage();
    $web_assert->fieldValueEquals('entities[' . $entity1->id() . '][weight]', 2);
    $web_assert->fieldValueEquals('entities[' . $entity2->id() . '][weight]', 1);
  }

  /**
   * Test DefaultConfigEntityListBuilder.
   *
   * @covers \Drupal\improvements\DefaultConfigEntityListBuilder
   */
  public function testDefaultConfigEntityListBuilder(): void {
    $this->installModule('improvements_test');
    $web_assert = $this->assertSession();
    $this->drupalLoginAsRoot();

    $entity1 = SecondConfigEntity::create(['id' => 'ent1', 'label' => 'Entity 1 for ' . __FUNCTION__]);
    $entity1->save();
    $entity2 = SecondConfigEntity::create(['id' => 'ent2', 'label' => 'Entity 2 for ' . __FUNCTION__]);
    $entity2->save();

    $this->drupalGet(Url::fromRoute('entity.second_config_entity.collection'));
    $this->dontSeeErrorMessage();
    $web_assert->pageTextContains($entity1->label());
    $web_assert->pageTextContains($entity2->label());
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc