Project

General

Profile

Paste
Download (9.19 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / views / tests / views_module.test @ 6de61b86

1
<?php
2

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

    
8
/**
9
 * Tests basic functions from the Views module.
10
 */
11
class ViewsModuleTest extends ViewsSqlTest {
12
  public static function getInfo() {
13
    return array(
14
      'name' => 'Tests views.module',
15
      'description' => 'Tests some basic functions of views.module',
16
      'group' => 'Views',
17
    );
18
  }
19

    
20
  public function setUp() {
21
    parent::setUp();
22
    drupal_theme_rebuild();
23
  }
24

    
25
  public function viewsData() {
26
    $data = parent::viewsData();
27
    $data['views_test_previous'] = array();
28
    $data['views_test_previous']['id']['field']['moved to'] = array('views_test', 'id');
29
    $data['views_test_previous']['id']['filter']['moved to'] = array('views_test', 'id');
30
    $data['views_test']['age_previous']['field']['moved to'] = array('views_test', 'age');
31
    $data['views_test']['age_previous']['sort']['moved to'] = array('views_test', 'age');
32
    $data['views_test_previous']['name_previous']['field']['moved to'] = array('views_test', 'name');
33
    $data['views_test_previous']['name_previous']['argument']['moved to'] = array('views_test', 'name');
34

    
35
    return $data;
36
  }
37

    
38
  public function test_views_trim_text() {
39
    // Test unicode, @see http://drupal.org/node/513396#comment-2839416
40
    $text = array(
41
      'Tuy nhiên, những hi vọng',
42
      'Giả sử chúng tôi có 3 Apple',
43
      'siêu nhỏ này là bộ xử lý',
44
      'Di động của nhà sản xuất Phần Lan',
45
      'khoảng cách từ đại lí đến',
46
      'của hãng bao gồm ba dòng',
47
      'сд асд асд ас',
48
      'асд асд асд ас',
49
    );
50
    // Just test maxlength without word boundry.
51
    $alter = array(
52
      'max_length' => 10,
53
    );
54
    $expect = array(
55
      'Tuy nhiên,',
56
      'Giả sử chú',
57
      'siêu nhỏ n',
58
      'Di động củ',
59
      'khoảng các',
60
      'của hãng b',
61
      'сд асд асд',
62
      'асд асд ас',
63
    );
64

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

    
70
    // Test also word_boundary
71
    $alter['word_boundary'] = TRUE;
72
    $expect = array(
73
      'Tuy nhiên',
74
      'Giả sử',
75
      'siêu nhỏ',
76
      'Di động',
77
      'khoảng',
78
      'của hãng',
79
      'сд асд',
80
      'асд асд',
81
    );
82

    
83
    foreach ($text as $key => $line) {
84
      $result_text = views_trim_text($alter, $line);
85
      $this->assertEqual($result_text, $expect[$key]);
86
    }
87
  }
88

    
89
  /**
90
   * Tests the dynamic includes of templates via module feature.
91
   */
92
  function testModuleTemplates() {
93
    $views_status = variable_get('views_defaults', array());
94
    $views_status['frontpage'] = FALSE;
95
    // false is enabled.
96
    variable_set('views_defaults', $views_status);
97

    
98
    $existing = array();
99
    $type = array();
100
    $theme = array();
101
    $path = array();
102
    $registry = views_theme($existing, $type, $theme, $path);
103
    $this->assertTrue(isset($registry['views_view__frontpage']));
104
  }
105

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

    
116
    $views_data = $this->viewsData();
117
    $test_tables = array('views_test' => array('id', 'name'));
118
    foreach ($test_tables as $table => $fields) {
119
      foreach ($fields as $field) {
120
        $data = $views_data[$table][$field];
121
        foreach ($data as $id => $field_data) {
122
          if (!in_array($id, array('title', 'help'))) {
123
            $handler = views_get_handler($table, $field, $id);
124
            $this->assertInstanceHandler($handler, $table, $field, $id);
125
          }
126
        }
127
      }
128
    }
129

    
130
    // Test the automatic conversion feature.
131
    // Test the automatic table renaming.
132
    $handler = views_get_handler('views_test_previous', 'id', 'field');
133
    $this->assertInstanceHandler($handler, 'views_test', 'id', 'field');
134
    $handler = views_get_handler('views_test_previous', 'id', 'filter');
135
    $this->assertInstanceHandler($handler, 'views_test', 'id', 'filter');
136

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

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

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

    
154
  /**
155
   * Tests views_fetch_data().
156
   */
157
  function testFetchData() {
158

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

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

    
174
    $this->resetStaticViewsDataCache();
175

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

    
181
    $this->resetStaticViewsDataCache();
182

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

    
189
    $this->resetStaticViewsDataCache();
190

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

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

    
205
    // Prime the static cache of _views_fetch_data() by calling it with a table
206
    // first.
207
    views_fetch_data('views_test');
208
    // Now remove the general cache.
209
    cache_clear_all('views_data:en', 'cache_views');
210
    // Reset the static cache to see if fetches from the persistent cache
211
    // properly rebuild the static cache.
212
    $this->resetStaticViewsDataCache();
213
    // Prime the static cache of _views_fetch_data() by calling it with a table
214
    // first.
215
    views_fetch_data('views_test');
216
    // Fetch the general cache, which was deleted, an see if it is rebuild
217
    // properly.
218
    views_fetch_data();
219
    $this->assertTrue(cache_get('views_data:en', 'cache_views'), 'Cache for all tables was properly rebuild.');
220
  }
221

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

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

    
232
  /**
233
   * Resets the views data cache.
234
   */
235
  protected function resetStaticViewsDataCache() {
236
    drupal_static_reset('_views_fetch_data_cache');
237
    drupal_static_reset('_views_fetch_data_recursion_protected');
238
    drupal_static_reset('_views_fetch_data_fully_loaded');
239
  }
240

    
241
}