annotate core/modules/views/src/Tests/ViewAjaxTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\views\Tests;
Chris@0 4
Chris@0 5 use Drupal\Component\Serialization\Json;
Chris@0 6 use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
Chris@0 7
Chris@0 8 /**
Chris@0 9 * Tests the ajax view functionality.
Chris@0 10 *
Chris@0 11 * @group views
Chris@0 12 */
Chris@0 13 class ViewAjaxTest extends ViewTestBase {
Chris@0 14
Chris@0 15 /**
Chris@0 16 * Views used by this test.
Chris@0 17 *
Chris@0 18 * @var array
Chris@0 19 */
Chris@0 20 public static $testViews = ['test_ajax_view', 'test_view'];
Chris@0 21
Chris@0 22 protected function setUp() {
Chris@0 23 parent::setUp();
Chris@0 24
Chris@0 25 $this->enableViewsTestModule();
Chris@0 26 }
Chris@0 27
Chris@0 28 /**
Chris@0 29 * Tests an ajax view.
Chris@0 30 */
Chris@0 31 public function testAjaxView() {
Chris@0 32 $this->drupalGet('test_ajax_view');
Chris@0 33
Chris@0 34 $drupal_settings = $this->getDrupalSettings();
Chris@0 35 $this->assertTrue(isset($drupal_settings['views']['ajax_path']), 'The Ajax callback path is set in drupalSettings.');
Chris@0 36 $this->assertEqual(count($drupal_settings['views']['ajaxViews']), 1);
Chris@0 37 $view_entry = array_keys($drupal_settings['views']['ajaxViews'])[0];
Chris@0 38 $this->assertEqual($drupal_settings['views']['ajaxViews'][$view_entry]['view_name'], 'test_ajax_view', 'The view\'s ajaxViews array entry has the correct \'view_name\' key.');
Chris@0 39 $this->assertEqual($drupal_settings['views']['ajaxViews'][$view_entry]['view_display_id'], 'page_1', 'The view\'s ajaxViews array entry has the correct \'view_display_id\' key.');
Chris@0 40
Chris@0 41 $data = [];
Chris@0 42 $data['view_name'] = 'test_ajax_view';
Chris@0 43 $data['view_display_id'] = 'test_ajax_view';
Chris@0 44
Chris@0 45 $post = [
Chris@0 46 'view_name' => 'test_ajax_view',
Chris@0 47 'view_display_id' => 'page_1',
Chris@0 48 ];
Chris@0 49 $post += $this->getAjaxPageStatePostData();
Chris@0 50 $response = $this->drupalPost('views/ajax', '', $post, ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax']]);
Chris@0 51 $data = Json::decode($response);
Chris@0 52
Chris@0 53 $this->assertTrue(isset($data[0]['settings']['views']['ajaxViews']));
Chris@0 54
Chris@0 55 // Ensure that the view insert command is part of the result.
Chris@0 56 $this->assertEqual($data[1]['command'], 'insert');
Chris@0 57 $this->assertTrue(strpos($data[1]['selector'], '.js-view-dom-id-') === 0);
Chris@0 58
Chris@0 59 $this->setRawContent($data[1]['data']);
Chris@0 60 $result = $this->xpath('//div[contains(@class, "views-row")]');
Chris@0 61 $this->assertEqual(count($result), 2, 'Ensure that two items are rendered in the HTML.');
Chris@0 62 }
Chris@0 63
Chris@0 64 /**
Chris@0 65 * Ensures that non-ajax view cannot be accessed via an ajax HTTP request.
Chris@0 66 */
Chris@0 67 public function testNonAjaxViewViaAjax() {
Chris@0 68 $this->drupalPost('views/ajax', '', ['view_name' => 'test_ajax_view', 'view_display_id' => 'default'], ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax']]);
Chris@0 69 $this->assertResponse(200);
Chris@0 70 $this->drupalPost('views/ajax', '', ['view_name' => 'test_view', 'view_display_id' => 'default'], ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax']]);
Chris@0 71 $this->assertResponse(403);
Chris@0 72 }
Chris@0 73
Chris@0 74 }