You are here

public function ViewExecutableTest::testPropertyMethods in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/views/tests/src/Kernel/ViewExecutableTest.php \Drupal\Tests\views\Kernel\ViewExecutableTest::testPropertyMethods()
  2. 9 core/modules/views/tests/src/Kernel/ViewExecutableTest.php \Drupal\Tests\views\Kernel\ViewExecutableTest::testPropertyMethods()

Tests the setting/getting of properties.

File

core/modules/views/tests/src/Kernel/ViewExecutableTest.php, line 294

Class

ViewExecutableTest
Tests the ViewExecutable class.

Namespace

Drupal\Tests\views\Kernel

Code

public function testPropertyMethods() {
  $view = Views::getView('test_executable_displays');

  // Test the setAjaxEnabled() method.
  $this
    ->assertFalse($view
    ->ajaxEnabled());
  $view
    ->setAjaxEnabled(TRUE);
  $this
    ->assertTrue($view
    ->ajaxEnabled());
  $view
    ->setDisplay();

  // There should be no pager set initially.
  $this
    ->assertNull($view
    ->usePager());

  // Add a pager, initialize, and test.
  $view->displayHandlers
    ->get('default')
    ->overrideOption('pager', [
    'type' => 'full',
    'options' => [
      'items_per_page' => 10,
    ],
  ]);
  $view
    ->initPager();
  $this
    ->assertTrue($view
    ->usePager());

  // Test setting and getting the offset.
  $rand = rand();
  $view
    ->setOffset($rand);
  $this
    ->assertEquals($rand, $view
    ->getOffset());

  // Test the getBaseTable() method.
  $expected = [
    'views_test_data' => TRUE,
    '#global' => TRUE,
  ];
  $this
    ->assertSame($expected, $view
    ->getBaseTables());

  // Test response methods.
  $this
    ->assertInstanceOf(Response::class, $view
    ->getResponse());
  $new_response = new Response();
  $view
    ->setResponse($new_response);
  $this
    ->assertSame(spl_object_hash($view
    ->getResponse()), spl_object_hash($new_response), 'New response object correctly set.');

  // Test the getPath() method.
  $path = $this
    ->randomMachineName();
  $view->displayHandlers
    ->get('page_1')
    ->overrideOption('path', $path);
  $view
    ->setDisplay('page_1');
  $this
    ->assertEquals($path, $view
    ->getPath());

  // Test the override_path property override.
  $override_path = $this
    ->randomMachineName();
  $view->override_path = $override_path;
  $this
    ->assertEquals($override_path, $view
    ->getPath());

  // Test the title methods.
  $title = $this
    ->randomString();
  $view
    ->setTitle($title);
  $this
    ->assertEquals(Xss::filterAdmin($title), $view
    ->getTitle());
}