Projet

Général

Profil

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

root / drupal7 / sites / all / modules / views / tests / views_ajax.test @ 4003efde

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
  /**
21
   * {@inheritdoc}
22
   */
23
  public function setUp(array $modules = array()) {
24
    parent::setUp($modules);
25

    
26
    // Create a second node.
27
    $this->drupalCreateNode(array('type' => 'article', 'status' => NODE_PUBLISHED));
28
  }
29

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

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

    
80
    $post = array(
81
      'view_name' => 'test_ajax_view',
82
      'view_display_id' => 'page_1',
83
    );
84

    
85
    $response = $this->simpleAjaxPost('views/ajax', 'application/json', $post);
86
    $data = drupal_json_decode($response);
87

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

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

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

    
98
    $post = array(
99
      'view_name' => 'test_noajax_view',
100
      'view_display_id' => 'default',
101
    );
102

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

    
113
}