views_ajax.test 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * @file
  4. * Definition of ViewsAjaxTest.
  5. */
  6. /**
  7. * Tests views ajax display.
  8. */
  9. class ViewsAjaxTest extends ViewsSqlTest {
  10. public static function getInfo() {
  11. return array(
  12. 'name' => 'Ajax',
  13. 'description' => 'Test views with and without ajax enabled.',
  14. 'group' => 'Views Handlers',
  15. );
  16. }
  17. public function setUp() {
  18. parent::setUp('views', 'views_test');
  19. // Create a second node.
  20. $this->drupalCreateNode(array('type' => 'article', 'status' => NODE_PUBLISHED));
  21. }
  22. /**
  23. * Perform a simple AJAX POST HTTP request.
  24. *
  25. * @param string $path
  26. * Drupal path where the request should be POSTed.
  27. * @param string $accept
  28. * The value for the "Accept" header. Usually either 'application/json' or
  29. * 'application/vnd.drupal-ajax'.
  30. * @param array $post
  31. * The POST data. When making a 'application/vnd.drupal-ajax' request, the
  32. * Ajax page state data should be included. Use getAjaxPageStatePostData()
  33. * for that.
  34. *
  35. * @return
  36. * The content returned from the call to curl_exec().
  37. */
  38. public function simpleAjaxPost($path, $accept, $post = array()) {
  39. $options['absolute'] = TRUE;
  40. foreach ($post as $key => $value) {
  41. // Encode according to application/x-www-form-urlencoded
  42. // Both names and values needs to be urlencoded, according to
  43. // http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4.1
  44. $post[$key] = urlencode($key) . '=' . urlencode($value);
  45. }
  46. $postfields = implode('&', $post);
  47. $headers = array(
  48. 'Accept: ' . $accept,
  49. 'Content-Type: application/x-www-form-urlencoded',
  50. );
  51. return $this->curlExec(array(
  52. CURLOPT_URL => url($path, $options),
  53. CURLOPT_POST => TRUE,
  54. CURLOPT_POSTFIELDS => $postfields,
  55. CURLOPT_HTTPHEADER => $headers,
  56. ));
  57. }
  58. /**
  59. * Tests an ajax and non-ajax view.
  60. */
  61. public function testAjaxView() {
  62. $this->drupalCreateNode();
  63. $this->drupalGet('test_ajax_view');
  64. $drupal_settings = $this->drupalGetSettings();
  65. $this->assertTrue(isset($drupal_settings['views']['ajax_path']), 'The Ajax callback path is set in drupalSettings.');
  66. $this->assertEqual(count($drupal_settings['views']['ajaxViews']), 1);
  67. $view_entry = current(array_keys($drupal_settings['views']['ajaxViews']));
  68. $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.');
  69. $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.');
  70. $post = array(
  71. 'view_name' => 'test_ajax_view',
  72. 'view_display_id' => 'page_1',
  73. );
  74. $response = $this->simpleAjaxPost('views/ajax', 'application/json', $post);
  75. $data = drupal_json_decode($response);
  76. $this->assertTrue(isset($data[0]['settings']['views']['ajaxViews']));
  77. // Ensure that the view insert command is part of the result.
  78. $this->assertEqual($data[1]['command'], 'insert');
  79. $this->assertTrue(strpos($data[1]['selector'], '.view-dom-id-') === 0);
  80. $this->drupalSetContent($data[1]['data']);
  81. $result = $this->xpath('//div[contains(@class, "views-row")]');
  82. $this->assertEqual(count($result), 2, 'Ensure that two items are rendered in the HTML.');
  83. $post = array(
  84. 'view_name' => 'test_noajax_view',
  85. 'view_display_id' => 'default',
  86. );
  87. $response = $this->simpleAjaxPost('views/ajax', 'application/json', $post);
  88. $data = drupal_json_decode($response);
  89. // In Drupal 7 we get an ajax response with no commands instead of a 403 if
  90. // the view cannot be accessed.
  91. foreach ($data as $item) {
  92. $this->assertIdentical('settings', $item['command']);
  93. $this->assertTrue(empty($item['data']));
  94. }
  95. }
  96. }