Projet

Général

Profil

Paste
Télécharger (3,79 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / tests / views_ajax.test @ 3acd948f

1
<?php
2

    
3
/**
4
 * @file
5
 * Definition of ViewsAjaxTest.
6
 */
7

    
8
/**
9
 * Tests views ajax display.
10
 */
11
class ViewsAjaxTest extends ViewsSqlTest {
12
  public static function getInfo() {
13
    return array(
14
      'name' => 'Ajax',
15
      'description' => 'Test views with and without ajax enabled.',
16
      'group' => 'Views Handlers',
17
    );
18
  }
19

    
20
  public function setUp() {
21
    parent::setUp('views', 'views_test');
22
    // Create a second node.
23
    $this->drupalCreateNode(array('type' => 'article', 'status' => NODE_PUBLISHED));
24
  }
25

    
26
  /**
27
   * Perform a simple AJAX POST HTTP request.
28
   *
29
   * @param string $path
30
   *   Drupal path where the request should be POSTed.
31
   * @param string $accept
32
   *   The value for the "Accept" header. Usually either 'application/json' or
33
   *   'application/vnd.drupal-ajax'.
34
   * @param array $post
35
   *   The POST data. When making a 'application/vnd.drupal-ajax' request, the
36
   *   Ajax page state data should be included. Use getAjaxPageStatePostData()
37
   *   for that.
38
   *
39
   * @return
40
   *   The content returned from the call to curl_exec().
41
   */
42
  public function simpleAjaxPost($path, $accept, $post = array()) {
43
    $options['absolute'] = TRUE;
44
    foreach ($post as $key => $value) {
45
      // Encode according to application/x-www-form-urlencoded
46
      // Both names and values needs to be urlencoded, according to
47
      // http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4.1
48
      $post[$key] = urlencode($key) . '=' . urlencode($value);
49
    }
50
    $postfields = implode('&', $post);
51
    $headers = array(
52
      'Accept: ' . $accept,
53
      'Content-Type: application/x-www-form-urlencoded',
54
    );
55
    return $this->curlExec(array(
56
      CURLOPT_URL => url($path, $options),
57
      CURLOPT_POST => TRUE,
58
      CURLOPT_POSTFIELDS => $postfields,
59
      CURLOPT_HTTPHEADER => $headers,
60
    ));
61
  }
62

    
63
  /**
64
   * Tests an ajax and non-ajax view.
65
   */
66
  public function testAjaxView() {
67
    $this->drupalCreateNode();
68
    $this->drupalGet('test_ajax_view');
69
    $drupal_settings = $this->drupalGetSettings();
70
    $this->assertTrue(isset($drupal_settings['views']['ajax_path']), 'The Ajax callback path is set in drupalSettings.');
71
    $this->assertEqual(count($drupal_settings['views']['ajaxViews']), 1);
72
    $view_entry = current(array_keys($drupal_settings['views']['ajaxViews']));
73
    $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.');
74
    $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.');
75

    
76
    $post = [
77
      'view_name' => 'test_ajax_view',
78
      'view_display_id' => 'page_1',
79
    ];
80

    
81
    $response = $this->simpleAjaxPost('views/ajax', 'application/json', $post);
82
    $data = drupal_json_decode($response);
83

    
84
    $this->assertTrue(isset($data[0]['settings']['views']['ajaxViews']));
85

    
86
    // Ensure that the view insert command is part of the result.
87
    $this->assertEqual($data[1]['command'], 'insert');
88
    $this->assertTrue(strpos($data[1]['selector'], '.view-dom-id-') === 0);
89

    
90
    $this->drupalSetContent($data[1]['data']);
91
    $result = $this->xpath('//div[contains(@class, "views-row")]');
92
    $this->assertEqual(count($result), 2, 'Ensure that two items are rendered in the HTML.');
93

    
94
    $post = [
95
      'view_name' => 'test_noajax_view',
96
      'view_display_id' => 'default',
97
    ];
98

    
99
    $response = $this->simpleAjaxPost('views/ajax', 'application/json', $post);
100
    $data = drupal_json_decode($response);
101
    // In Drupal 7 we get an ajax response with no commands instead of a 403 if
102
    // the view cannot be accessed.
103
    foreach ($data as $item) {
104
      $this->assertIdentical('settings', $item['command']);
105
      $this->assertTrue(empty($item['data']));
106
    }
107
  }
108

    
109
}