ModuleTest.php 14.5 KB
Newer Older
merlinofchaos's avatar
merlinofchaos committed
1 2 3 4
<?php

/**
 * @file
5
 * Definition of Drupal\views\Tests\ModuleTest.
merlinofchaos's avatar
merlinofchaos committed
6 7
 */

8 9
namespace Drupal\views\Tests;

merlinofchaos's avatar
merlinofchaos committed
10 11
/**
 * Tests basic functions from the Views module.
12 13
 *
 * @group views
merlinofchaos's avatar
merlinofchaos committed
14
 */
15
use Drupal\views\Plugin\views\filter\Standard;
16
use Drupal\views\Views;
17
use Drupal\Component\Utility\SafeMarkup;
18

19
class ModuleTest extends ViewUnitTestBase {
20

21 22 23 24 25
  /**
   * Views used by this test.
   *
   * @var array
   */
26
  public static $testViews = array('test_view_status', 'test_view', 'test_argument');
27

28 29 30 31 32
  /**
   * Modules to enable.
   *
   * @var array
   */
33
  public static $modules = ['field', 'user', 'block'];
34

35 36 37 38 39 40 41 42 43
  /**
   * Stores the last triggered error, for example via debug().
   *
   * @var string
   *
   * @see \Drupal\views\Tests\ModuleTest::errorHandler()
   */
  protected $lastErrorMessage;

merlinofchaos's avatar
merlinofchaos committed
44 45
  /**
   * Tests the views_get_handler method.
46 47
   *
   * @see views_get_handler()
merlinofchaos's avatar
merlinofchaos committed
48
   */
49
  public function testViewsGetHandler() {
merlinofchaos's avatar
merlinofchaos committed
50 51
    $types = array('field', 'area', 'filter');
    foreach ($types as $type) {
52
      $item = array(
53 54
        'table' => $this->randomMachineName(),
        'field' => $this->randomMachineName(),
55
      );
56
      $handler = $this->container->get('plugin.manager.views.' . $type)->getHandler($item);
aspilicious's avatar
aspilicious committed
57
      $this->assertEqual('Drupal\views\Plugin\views\\' . $type . '\Broken', get_class($handler), t('Make sure that a broken handler of type: @type are created', array('@type' => $type)));
merlinofchaos's avatar
merlinofchaos committed
58 59 60
    }

    $views_data = $this->viewsData();
61
    $test_tables = array('views_test_data' => array('id', 'name'));
merlinofchaos's avatar
merlinofchaos committed
62 63 64
    foreach ($test_tables as $table => $fields) {
      foreach ($fields as $field) {
        $data = $views_data[$table][$field];
65 66 67 68
        $item = array(
          'table' => $table,
          'field' => $field,
        );
merlinofchaos's avatar
merlinofchaos committed
69 70
        foreach ($data as $id => $field_data) {
          if (!in_array($id, array('title', 'help'))) {
71
            $handler = $this->container->get('plugin.manager.views.' . $id)->getHandler($item);
merlinofchaos's avatar
merlinofchaos committed
72 73 74 75 76 77
            $this->assertInstanceHandler($handler, $table, $field, $id);
          }
        }
      }
    }

78
    // Test the override handler feature.
79 80 81 82
    $item = array(
      'table' => 'views_test_data',
      'field' => 'job',
    );
83
    $handler = $this->container->get('plugin.manager.views.filter')->getHandler($item, 'standard');
84 85
    $this->assertTrue($handler instanceof Standard);

86 87 88 89
    // @todo Reinstate these tests when the debug() in views_get_handler() is
    //   restored.
    return;

90 91 92 93 94 95
    // Test non-existent tables/fields.
    set_error_handler(array($this, 'customErrorHandler'));
    $item = array(
      'table' => 'views_test_data',
      'field' => 'field_invalid',
    );
96
    $this->container->get('plugin.manager.views.field')->getHandler($item);
97 98 99 100 101 102 103
    $this->assertTrue(strpos($this->lastErrorMessage, format_string("Missing handler: @table @field @type", array('@table' => 'views_test_data', '@field' => 'field_invalid', '@type' => 'field'))) !== FALSE, 'An invalid field name throws a debug message.');
    unset($this->lastErrorMessage);

    $item = array(
      'table' => 'table_invalid',
      'field' => 'id',
    );
104
    $this->container->get('plugin.manager.views.filter')->getHandler($item);
105 106 107 108 109 110 111
    $this->assertEqual(strpos($this->lastErrorMessage, format_string("Missing handler: @table @field @type", array('@table' => 'table_invalid', '@field' => 'id', '@type' => 'filter'))) !== FALSE, 'An invalid table name throws a debug message.');
    unset($this->lastErrorMessage);

    $item = array(
      'table' => 'table_invalid',
      'field' => 'id',
    );
112
    $this->container->get('plugin.manager.views.filter')->getHandler($item);
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
    $this->assertEqual(strpos($this->lastErrorMessage, format_string("Missing handler: @table @field @type", array('@table' => 'table_invalid', '@field' => 'id', '@type' => 'filter'))) !== FALSE, 'An invalid table name throws a debug message.');
    unset($this->lastErrorMessage);

    restore_error_handler();
  }

  /**
   * Defines an error handler which is used in the test.
   *
   * @param int $error_level
   *   The level of the error raised.
   * @param string $message
   *   The error message.
   * @param string $filename
   *   The filename that the error was raised in.
   * @param int $line
   *   The line number the error was raised at.
   * @param array $context
   *   An array that points to the active symbol table at the point the error
   *   occurred.
   *
   * Because this is registered in set_error_handler(), it has to be public.
   * @see set_error_handler()
   */
  public function customErrorHandler($error_level, $message, $filename, $line, $context) {
    $this->lastErrorMessage = $message;
merlinofchaos's avatar
merlinofchaos committed
139 140
  }

141 142 143 144
  /**
   * Tests the load wrapper/helper functions.
   */
  public function testLoadFunctions() {
145 146
    $this->enableModules(array('field', 'text', 'node'));
    $this->installConfig(array('node'));
147
    $storage = $this->container->get('entity.manager')->getStorage('view');
148 149

    // Test views_view_is_enabled/disabled.
150
    $archive = $storage->load('archive');
151 152 153 154 155 156
    $this->assertTrue(views_view_is_disabled($archive), 'views_view_is_disabled works as expected.');
    // Enable the view and check this.
    $archive->enable();
    $this->assertTrue(views_view_is_enabled($archive), ' views_view_is_enabled works as expected.');

    // We can store this now, as we have enabled/disabled above.
157
    $all_views = $storage->loadMultiple();
158

159 160
    // Test Views::getAllViews().
    $this->assertIdentical(array_keys($all_views), array_keys(Views::getAllViews()), 'Views::getAllViews works as expected.');
161

162
    // Test Views::getEnabledViews().
163 164 165
    $expected_enabled = array_filter($all_views, function($view) {
      return views_view_is_enabled($view);
    });
166
    $this->assertIdentical(array_keys($expected_enabled), array_keys(Views::getEnabledViews()), 'Expected enabled views returned.');
167

168
    // Test Views::getDisabledViews().
169 170 171
    $expected_disabled = array_filter($all_views, function($view) {
      return views_view_is_disabled($view);
    });
172
    $this->assertIdentical(array_keys($expected_disabled), array_keys(Views::getDisabledViews()), 'Expected disabled views returned.');
173

174
    // Test Views::getViewsAsOptions().
175
    // Test the $views_only parameter.
176
    $this->assertIdentical(array_keys($all_views), array_keys(Views::getViewsAsOptions(TRUE)), 'Expected option keys for all views were returned.');
177 178
    $expected_options = array();
    foreach ($all_views as $id => $view) {
179
      $expected_options[$id] = $view->label();
180
    }
181
    $this->assertIdentical($expected_options, Views::getViewsAsOptions(TRUE), 'Expected options array was returned.');
182 183

    // Test the default.
184
    $this->assertIdentical($this->formatViewOptions($all_views), Views::getViewsAsOptions(), 'Expected options array for all views was returned.');
185
    // Test enabled views.
186
    $this->assertIdentical($this->formatViewOptions($expected_enabled), Views::getViewsAsOptions(FALSE, 'enabled'), 'Expected enabled options array was returned.');
187
    // Test disabled views.
188
    $this->assertIdentical($this->formatViewOptions($expected_disabled), Views::getViewsAsOptions(FALSE, 'disabled'), 'Expected disabled options array was returned.');
189 190 191 192

    // Test the sort parameter.
    $all_views_sorted = $all_views;
    ksort($all_views_sorted);
193
    $this->assertIdentical(array_keys($all_views_sorted), array_keys(Views::getViewsAsOptions(TRUE, 'all', NULL, FALSE, TRUE)), 'All view id keys returned in expected sort order');
194 195

    // Test $exclude_view parameter.
196 197 198
    $this->assertFalse(array_key_exists('archive', Views::getViewsAsOptions(TRUE, 'all', 'archive')), 'View excluded from options based on name');
    $this->assertFalse(array_key_exists('archive:default', Views::getViewsAsOptions(FALSE, 'all', 'archive:default')), 'View display excluded from options based on name');
    $this->assertFalse(array_key_exists('archive', Views::getViewsAsOptions(TRUE, 'all', $archive->getExecutable())), 'View excluded from options based on object');
199 200 201

    // Test the $opt_group parameter.
    $expected_opt_groups = array();
202 203
    foreach ($all_views as $view) {
      foreach ($view->get('display') as $display) {
204 205 206
          $expected_opt_groups[$view->id()][$view->id() . ':' . $display['id']] = t('@view : @display', array('@view' => $view->id(), '@display' => $display['id']));
      }
    }
207
    $this->assertIdentical($expected_opt_groups, Views::getViewsAsOptions(FALSE, 'all', NULL, TRUE), 'Expected option array for an option group returned.');
208 209 210 211 212 213
  }

  /**
   * Tests view enable and disable procedural wrapper functions.
   */
  function testStatusFunctions() {
214
    $view = Views::getView('test_view_status')->storage;
215

216
    $this->assertFalse($view->status(), 'The view status is disabled.');
217 218

    views_enable_view($view);
219 220
    $this->assertTrue($view->status(), 'A view has been enabled.');
    $this->assertEqual($view->status(), views_view_is_enabled($view), 'views_view_is_enabled is correct.');
221 222

    views_disable_view($view);
223 224
    $this->assertFalse($view->status(), 'A view has been disabled.');
    $this->assertEqual(!$view->status(), views_view_is_disabled($view), 'views_view_is_disabled is correct.');
225 226
  }

227
  /**
228
   * Tests the \Drupal\views\Views::fetchPluginNames() method.
229 230 231
   */
  public function testViewsFetchPluginNames() {
    // All style plugins should be returned, as we have not specified a type.
232
    $plugins = Views::fetchPluginNames('style');
233
    $definitions = $this->container->get('plugin.manager.views.style')->getDefinitions();
234 235 236 237 238 239 240 241 242
    $expected = array();
    foreach ($definitions as $id =>$definition) {
      $expected[$id] = $definition['title'];
    }
    asort($expected);
    $this->assertIdentical(array_keys($plugins), array_keys($expected));

    // Test using the 'test' style plugin type only returns the test_style and
    // mapping_test plugins.
243
    $plugins = Views::fetchPluginNames('style', 'test');
244
    $this->assertIdentical(array_keys($plugins), array('mapping_test', 'test_style', 'test_template_style'));
245 246

    // Test a non existent style plugin type returns no plugins.
247
    $plugins = Views::fetchPluginNames('style', $this->randomString());
248 249 250
    $this->assertIdentical($plugins, array());
  }

251
  /**
252
   * Tests the \Drupal\views\Views::pluginList() method.
253 254
   */
  public function testViewsPluginList() {
255
    $plugin_list = Views::pluginList();
256 257 258 259 260
    // Only plugins used by 'test_view' should be in the plugin list.
    foreach (array('display:default', 'pager:none') as $key) {
      list($plugin_type, $plugin_id) = explode(':', $key);
      $plugin_def = $this->container->get("plugin.manager.views.$plugin_type")->getDefinition($plugin_id);

261
      $this->assertTrue(isset($plugin_list[$key]), SafeMarkup::format('The expected @key plugin list key was found.', array('@key' => $key)));
262 263 264 265 266 267 268 269 270
      $plugin_details = $plugin_list[$key];

      $this->assertEqual($plugin_details['type'], $plugin_type, 'The expected plugin type was found.');
      $this->assertEqual($plugin_details['title'], $plugin_def['title'], 'The expected plugin title was found.');
      $this->assertEqual($plugin_details['provider'], $plugin_def['provider'], 'The expected plugin provider was found.');
      $this->assertTrue(in_array('test_view', $plugin_details['views']), 'The test_view View was found in the list of views using this plugin.');
    }
  }

271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
  /**
   * Tests views.module: views_embed_view().
   */
  public function testViewsEmbedView() {
    $this->enableModules(array('user'));

    $result = views_embed_view('test_argument');
    $this->assertEqual(count($result['#view']->result), 5);

    $result = views_embed_view('test_argument', 'default', 1);
    $this->assertEqual(count($result['#view']->result), 1);

    $result = views_embed_view('test_argument', 'default', '1,2');
    $this->assertEqual(count($result['#view']->result), 2);

    $result = views_embed_view('test_argument', 'default', '1,2', 'John');
    $this->assertEqual(count($result['#view']->result), 1);

    $result = views_embed_view('test_argument', 'default', '1,2', 'John,George');
    $this->assertEqual(count($result['#view']->result), 2);
  }

  /**
   * Tests the \Drupal\views\ViewsExecutable::preview() method.
   */
  public function testViewsPreview() {
    $this->enableModules(array('user'));

    $view = Views::getView('test_argument');
    $result = $view->preview('default');
    $this->assertEqual(count($result['#view']->result), 5);

    $view = Views::getView('test_argument');
    $result = $view->preview('default', array('0' => 1));
    $this->assertEqual(count($result['#view']->result), 1);

    $view = Views::getView('test_argument');
    $result = $view->preview('default', array('3' => 1));
    $this->assertEqual(count($result['#view']->result), 1);

    $view = Views::getView('test_argument');
    $result = $view->preview('default', array('0' => '1,2'));
    $this->assertEqual(count($result['#view']->result), 2);

    $view = Views::getView('test_argument');
    $result = $view->preview('default', array('3' => '1,2'));
    $this->assertEqual(count($result['#view']->result), 2);

    $view = Views::getView('test_argument');
    $result = $view->preview('default', array('0' => '1,2', '1' => 'John'));
    $this->assertEqual(count($result['#view']->result), 1);

    $view = Views::getView('test_argument');
    $result = $view->preview('default', array('3' => '1,2', '4' => 'John'));
    $this->assertEqual(count($result['#view']->result), 1);

    $view = Views::getView('test_argument');
    $result = $view->preview('default', array('0' => '1,2', '1' => 'John,George'));
    $this->assertEqual(count($result['#view']->result), 2);

    $view = Views::getView('test_argument');
    $result = $view->preview('default', array('3' => '1,2', '4' => 'John,George'));
    $this->assertEqual(count($result['#view']->result), 2);
  }

336 337 338 339
  /**
   * Helper to return an expected views option array.
   *
   * @param array $views
340
   *   An array of Drupal\views\Entity\View objects for which to
341
   *   create an options array.
342 343 344 345 346 347
   *
   * @return array
   *   A formatted options array that matches the expected output.
   */
  protected function formatViewOptions(array $views = array()) {
    $expected_options = array();
348 349
    foreach ($views as $view) {
      foreach ($view->get('display') as $display) {
350
        $expected_options[$view->id() . ':' . $display['id']] = t('View: @view - Display: @display',
351
          array('@view' => $view->id(), '@display' => $display['id']));
352 353 354 355 356 357
      }
    }

    return $expected_options;
  }

merlinofchaos's avatar
merlinofchaos committed
358 359 360 361
  /**
   * Ensure that a certain handler is a instance of a certain table/field.
   */
  function assertInstanceHandler($handler, $table, $field, $id) {
362
    $table_data = $this->container->get('views.views_data')->get($table);
merlinofchaos's avatar
merlinofchaos committed
363 364
    $field_data = $table_data[$field][$id];

aspilicious's avatar
aspilicious committed
365
    $this->assertEqual($field_data['id'], $handler->getPluginId());
merlinofchaos's avatar
merlinofchaos committed
366
  }
367

merlinofchaos's avatar
merlinofchaos committed
368
}