ViewsHooksTest.php 3.41 KB
Newer Older
1 2
<?php

3
namespace Drupal\Tests\views\Kernel;
4

5
use Drupal\Core\Render\RenderContext;
6 7
use Drupal\views\Views;

8 9 10
/**
 * Tests that views hooks are registered when defined in $module.views.inc.
 *
11
 * @group views
12 13 14
 * @see views_hook_info().
 * @see field_hook_info().
 */
15
class ViewsHooksTest extends ViewsKernelTestBase {
16 17

  /**
18
   * Views used by this test.
19 20 21
   *
   * @var array
   */
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
  public static $testViews = array('test_view');

  /**
   * An array of available views hooks to test.
   *
   * @var array
   */
  protected static $hooks = array (
    'views_data' => 'all',
    'views_data_alter' => 'alter',
    'views_query_substitutions' => 'view',
    'views_form_substitutions' => 'view',
    'views_analyze' => 'view',
    'views_pre_view' => 'view',
    'views_pre_build' => 'view',
    'views_post_build' => 'view',
    'views_pre_execute' => 'view',
    'views_post_execute' => 'view',
    'views_pre_render' => 'view',
    'views_post_render' => 'view',
    'views_query_alter'  => 'view',
    'views_invalidate_cache' => 'all',
  );

  /**
   * The module handler to use for invoking hooks.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;
52

53
  protected function setUp($import_test_views = TRUE) {
54 55 56 57 58
    parent::setUp();

    $this->moduleHandler = $this->container->get('module_handler');
  }

59 60 61 62
  /**
   * Tests the hooks.
   */
  public function testHooks() {
63
    $view = Views::getView('test_view');
64
    $view->setDisplay();
65 66 67 68 69

    // Test each hook is found in the implementations array and is invoked.
    foreach (static::$hooks as $hook => $type) {
      $this->assertTrue($this->moduleHandler->implementsHook('views_test_data', $hook), format_string('The hook @hook was registered.', array('@hook' => $hook)));

70 71 72 73 74
      if ($hook == 'views_post_render') {
        $this->moduleHandler->invoke('views_test_data', $hook, array($view, &$view->display_handler->output, $view->display_handler->getPlugin('cache')));
        continue;
      }

75 76 77 78 79 80 81 82 83
      switch ($type) {
        case 'view':
          $this->moduleHandler->invoke('views_test_data', $hook, array($view));
          break;

        case 'alter':
          $data = array();
          $this->moduleHandler->invoke('views_test_data', $hook, array($data));
          break;
84

85 86 87
        default:
          $this->moduleHandler->invoke('views_test_data', $hook);
      }
88

89 90 91 92
      $this->assertTrue($this->container->get('state')->get('views_hook_test_' . $hook), format_string('The %hook hook was invoked.', array('%hook' => $hook)));
      // Reset the module implementations cache, so we ensure that the
      // .views.inc file is loaded actively.
      $this->moduleHandler->resetImplementations();
93 94 95
    }
  }

96 97 98 99 100 101 102 103 104
  /**
   * Tests how hook_views_form_substitutions() makes substitutions.
   *
   * @see views_test_data_views_form_substitutions()
   * @see views_pre_render_views_form_views_form()
   */
  public function testViewsPreRenderViewsFormViewsForm() {
    $element = [
      'output' => [
105
        '#plain_text' => '<!--will-be-escaped--><!--will-be-not-escaped-->',
106 107 108 109 110 111 112 113 114 115 116
      ],
      '#substitutions' => ['#value' => []],
    ];
    $element = \Drupal::service('renderer')->executeInRenderContext(new RenderContext(), function() use ($element) {
      return views_pre_render_views_form_views_form($element);
    });
    $this->setRawContent((string) $element['output']['#markup']);
    $this->assertEscaped('<em>escaped</em>');
    $this->assertRaw('<em>unescaped</em>');
  }

117
}