You are here

public function ViewsAjaxTest::testAjaxView in Views (for Drupal 7) 7.3

Tests an ajax and non-ajax view.

File

tests/views_ajax.test, line 70
Definition of ViewsAjaxTest.

Class

ViewsAjaxTest
Tests views ajax display.

Code

public function testAjaxView() {
  $this
    ->drupalCreateNode();
  $this
    ->drupalGet('test_ajax_view');
  $drupal_settings = $this
    ->drupalGetSettings();
  $this
    ->assertTrue(isset($drupal_settings['views']['ajax_path']), 'The Ajax callback path is set in drupalSettings.');
  $this
    ->assertEqual(count($drupal_settings['views']['ajaxViews']), 1);
  $view_entry = current(array_keys($drupal_settings['views']['ajaxViews']));
  $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.');
  $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.');
  $post = array(
    'view_name' => 'test_ajax_view',
    'view_display_id' => 'page_1',
  );
  $response = $this
    ->simpleAjaxPost('views/ajax', 'application/json', $post);
  $data = drupal_json_decode($response);
  $this
    ->assertTrue(isset($data[0]['settings']['views']['ajaxViews']));

  // Ensure that the view insert command is part of the result.
  $this
    ->assertEqual($data[1]['command'], 'insert');
  $this
    ->assertTrue(strpos($data[1]['selector'], '.view-dom-id-') === 0);
  $this
    ->drupalSetContent($data[1]['data']);
  $result = $this
    ->xpath('//div[contains(@class, "views-row")]');
  $this
    ->assertEqual(count($result), 2, 'Ensure that two items are rendered in the HTML.');
  $post = array(
    'view_name' => 'test_noajax_view',
    'view_display_id' => 'default',
  );
  $response = $this
    ->simpleAjaxPost('views/ajax', 'application/json', $post);
  $data = drupal_json_decode($response);

  // In Drupal 7 we get an ajax response with no commands instead of a 403 if
  // the view cannot be accessed.
  foreach ($data as $item) {
    $this
      ->assertIdentical('settings', $item['command']);
    $this
      ->assertTrue(empty($item['data']));
  }
}