summaryrefslogtreecommitdiff
path: root/sites/all/modules/views/tests/views_module.test
blob: e160964a90e9a5e85cf6e88bb15fb6ab771bb8ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?php

/**
 * @file
 * Definition of ViewsModuleTest.
 */

/**
 * Tests basic functions from the Views module.
 */
class ViewsModuleTest extends ViewsSqlTest {
  public static function getInfo() {
    return array(
      'name' => 'Tests views.module',
      'description' => 'Tests some basic functions of views.module',
      'group' => 'Views',
    );
  }

  public function setUp() {
    parent::setUp();
    drupal_theme_rebuild();
  }

  public function viewsData() {
    $data = parent::viewsData();
    $data['views_test_previous'] = array();
    $data['views_test_previous']['id']['field']['moved to'] = array('views_test', 'id');
    $data['views_test_previous']['id']['filter']['moved to'] = array('views_test', 'id');
    $data['views_test']['age_previous']['field']['moved to'] = array('views_test', 'age');
    $data['views_test']['age_previous']['sort']['moved to'] = array('views_test', 'age');
    $data['views_test_previous']['name_previous']['field']['moved to'] = array('views_test', 'name');
    $data['views_test_previous']['name_previous']['argument']['moved to'] = array('views_test', 'name');

    return $data;
  }

  public function test_views_trim_text() {
    // Test unicode, @see http://drupal.org/node/513396#comment-2839416
    $text = array(
      'Tuy nhiên, những hi vọng',
      'Giả sử chúng tôi có 3 Apple',
      'siêu nhỏ này là bộ xử lý',
      'Di động của nhà sản xuất Phần Lan',
      'khoảng cách từ đại lí đến',
      'của hãng bao gồm ba dòng',
      'сд асд асд ас',
      'асд асд асд ас'
    );
    // Just test maxlength without word boundry.
    $alter = array(
      'max_length' => 10,
    );
    $expect = array(
      'Tuy nhiên,',
      'Giả sử chú',
      'siêu nhỏ n',
      'Di động củ',
      'khoảng các',
      'của hãng b',
      'сд асд асд',
      'асд асд ас',
    );

    foreach ($text as $key => $line) {
      $result_text = views_trim_text($alter, $line);
      $this->assertEqual($result_text, $expect[$key]);
    }

    // Test also word_boundary
    $alter['word_boundary'] = TRUE;
    $expect = array(
      'Tuy nhiên',
      'Giả sử',
      'siêu nhỏ',
      'Di động',
      'khoảng',
      'của hãng',
      'сд асд',
      'асд асд',
    );

    foreach ($text as $key => $line) {
      $result_text = views_trim_text($alter, $line);
      $this->assertEqual($result_text, $expect[$key]);
    }
  }

  /**
   * Tests the dynamic includes of templates via module feature.
   */
  function testModuleTemplates() {
    $views_status = variable_get('views_defaults', array());
    $views_status['frontpage'] = FALSE; // false is enabled
    variable_set('views_defaults', $views_status);

    $existing = array();
    $type = array();
    $theme = array();
    $path = array();
    $registry = views_theme($existing, $type, $theme, $path);
    $this->assertTrue(isset($registry['views_view__frontpage']));
  }

  /**
   * Tests the views_get_handler method.
   */
  function testviews_get_handler() {
    $types = array('field', 'area', 'filter');
    foreach ($types as $type) {
      $handler = views_get_handler($this->randomName(), $this->randomName(), $type);
      $this->assertEqual('views_handler_' . $type . '_broken', get_class($handler), t('Make sure that a broken handler of type: @type are created', array('@type' => $type)));
    }

    $views_data = $this->viewsData();
    $test_tables = array('views_test' => array('id', 'name'));
    foreach ($test_tables as $table => $fields) {
      foreach ($fields as $field) {
        $data = $views_data[$table][$field];
        foreach ($data as $id => $field_data) {
          if (!in_array($id, array('title', 'help'))) {
            $handler = views_get_handler($table, $field, $id);
            $this->assertInstanceHandler($handler, $table, $field, $id);
          }
        }
      }
    }

    // Test the automatic conversion feature.

    // Test the automatic table renaming.
    $handler = views_get_handler('views_test_previous', 'id', 'field');
    $this->assertInstanceHandler($handler, 'views_test', 'id', 'field');
    $handler = views_get_handler('views_test_previous', 'id', 'filter');
    $this->assertInstanceHandler($handler, 'views_test', 'id', 'filter');

    // Test the automatic field renaming.
    $handler = views_get_handler('views_test', 'age_previous', 'field');
    $this->assertInstanceHandler($handler, 'views_test', 'age', 'field');
    $handler = views_get_handler('views_test', 'age_previous', 'sort');
    $this->assertInstanceHandler($handler, 'views_test', 'age', 'sort');

    // Test the automatic table and field renaming.
    $handler = views_get_handler('views_test_previous', 'name_previous', 'field');
    $this->assertInstanceHandler($handler, 'views_test', 'name', 'field');
    $handler = views_get_handler('views_test_previous', 'name_previous', 'argument');
    $this->assertInstanceHandler($handler, 'views_test', 'name', 'argument');

    // Test the override handler feature.
    $handler = views_get_handler('views_test', 'job', 'filter', 'views_handler_filter');
    $this->assertEqual('views_handler_filter', get_class($handler));
  }

  /**
   * Tests views_fetch_data().
   */
  function testFetchData() {

    // Make sure we start with a empty cache.
    $this->resetStaticViewsDataCache();
    cache_clear_all('*', 'cache_views', TRUE);
    variable_set('views_test_views_data_count', 0);

    // Request info about an existing table.
    $this->assertTrue(views_fetch_data('views_test'), 'Data about existing table returned');
    // This should have triggered a views data rebuild, and written a cache
    // entry for all tables and the requested table but no other tables.
    $this->assertEqual(variable_get('views_test_views_data_count', 0), 1, 'Views data rebuilt once');
    $this->assertTrue(cache_get('views_data:en', 'cache_views'), 'Cache for all tables written.');
    $this->assertTrue(cache_get('views_data:views_test:en', 'cache_views'), 'Cache for requested table written.');
    $this->assertFalse(cache_get('views_data:views_test_previous:en', 'cache_views'), 'No Cache written for not requested table.');
    $this->assertTrue(drupal_static('_views_fetch_data_fully_loaded'), 'Views data is fully loaded');

    $this->resetStaticViewsDataCache();

    // Request the same table again.
    $this->assertTrue(views_fetch_data('views_test'), 'Data about existing table returned');
    $this->assertEqual(variable_get('views_test_views_data_count', 0), 1, 'Views data rebuilt once');
    $this->assertFalse(drupal_static('_views_fetch_data_fully_loaded'), 'Views data is not fully loaded');

    $this->resetStaticViewsDataCache();

    // Request a missing table, this should load the full cache from cache but
    // not rebuilt.
    $this->assertFalse(views_fetch_data('views_test_missing'), 'No data about missing table returned');
    $this->assertEqual(variable_get('views_test_views_data_count', 0), 1, 'Views data rebuilt once');
    $this->assertTrue(drupal_static('_views_fetch_data_fully_loaded'), 'Views data is fully loaded');

    $this->resetStaticViewsDataCache();

    // Request the same empty table again, this should load only that (empty)
    // cache for that table.
    $this->assertFalse(views_fetch_data('views_test_missing'), 'No data about missing table returned');
    $this->assertEqual(variable_get('views_test_views_data_count', 0), 1, 'Views data rebuilt once');
    $this->assertFalse(drupal_static('_views_fetch_data_fully_loaded'), 'Views data is not fully loaded');


    // Test if the cache consistency is ensured. There was an issue where
    // calling _views_fetch_data() first with a table would prevent the function
    // from properly rebuilt a missing the general cache entry.
    // See https://www.drupal.org/node/2475669 for details.
    // Make sure we start with a empty cache.
    $this->resetStaticViewsDataCache();
    cache_clear_all('*', 'cache_views', TRUE);

    // Prime the static cache of _views_fetch_data() by calling it with a table
    // first.
    views_fetch_data('views_test');
    // Now remove the general cache.
    cache_clear_all('views_data:en', 'cache_views');
    // Reset the static cache to see if fetches from the persistent cache
    // properly rebuild the static cache.
    $this->resetStaticViewsDataCache();
    // Prime the static cache of _views_fetch_data() by calling it with a table
    // first.
    views_fetch_data('views_test');
    // Fetch the general cache, which was deleted, an see if it is rebuild
    // properly.
    views_fetch_data();
    $this->assertTrue(cache_get('views_data:en', 'cache_views'), 'Cache for all tables was properly rebuild.');
  }

  /**
   * Ensure that a certain handler is a instance of a certain table/field.
   */
  function assertInstanceHandler($handler, $table, $field, $id) {
    $table_data = views_fetch_data($table);
    $field_data = $table_data[$field][$id];

    $this->assertEqual($field_data['handler'], get_class($handler));
  }

  /**
   * Resets the views data cache.
   */
  protected function resetStaticViewsDataCache() {
    drupal_static_reset('_views_fetch_data_cache');
    drupal_static_reset('_views_fetch_data_recursion_protected');
    drupal_static_reset('_views_fetch_data_fully_loaded');
  }
}