Project

General

Profile

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

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

1
<?php
2

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

    
8
/**
9
 * Tests the pluggable pager system.
10
 */
11
class ViewsPagerTest extends ViewsSqlTest {
12

    
13
  /**
14
   *
15
   */
16
  public static function getInfo() {
17
    return array(
18
      'name' => 'Pager',
19
      'description' => 'Test the pluggable pager system',
20
      'group' => 'Views Plugins',
21
    );
22
  }
23

    
24
  /**
25
   *
26
   */
27
  public function setUp() {
28
    parent::setUp('views', 'views_ui', 'views_test');
29
  }
30

    
31
  /**
32
   * Pagers was sometimes not stored.
33
   *
34
   * @see http://drupal.org/node/652712
35
   */
36
  public function testStorePagerSettings() {
37
    $admin_user = $this->drupalCreateUser(array('administer views', 'administer site configuration'));
38
    $this->drupalLogin($admin_user);
39
    // Test behaviour described in
40
    // http://drupal.org/node/652712#comment-2354918.
41
    $this->drupalGet('admin/structure/views/view/frontpage/edit');
42

    
43
    $edit = array(
44
      'pager_options[items_per_page]' => 20,
45
    );
46
    $this->drupalPost('admin/structure/views/nojs/display/frontpage/default/pager_options', $edit, t('Apply'));
47
    $this->assertText('20 items');
48

    
49
    // Change type and check whether the type is new type is stored.
50
    $edit = array();
51
    $edit = array(
52
      'pager[type]' => 'mini',
53
    );
54
    $this->drupalPost('admin/structure/views/nojs/display/frontpage/default/pager', $edit, t('Apply'));
55
    $this->drupalGet('admin/structure/views/view/frontpage/edit');
56
    $this->assertText('Mini', 'Changed pager plugin, should change some text');
57

    
58
    // Test behaviour described in http://drupal.org/node/652712#comment-2354400
59
    $view = $this->viewsStorePagerSettings();
60
    // Make it editable in the admin interface.
61
    $view->save();
62

    
63
    $this->drupalGet('admin/structure/views/view/test_store_pager_settings/edit');
64

    
65
    $edit = array();
66
    $edit = array(
67
      'pager[type]' => 'full',
68
    );
69
    $this->drupalPost('admin/structure/views/nojs/display/test_store_pager_settings/default/pager', $edit, t('Apply'));
70
    $this->drupalGet('admin/structure/views/view/test_store_pager_settings/edit');
71
    $this->assertText('Full');
72

    
73
    $edit = array(
74
      'pager_options[items_per_page]' => 20,
75
    );
76
    $this->drupalPost('admin/structure/views/nojs/display/test_store_pager_settings/default/pager_options', $edit, t('Apply'));
77
    $this->assertText('20 items');
78

    
79
    // Add new display and test the settings again, by override it.
80
    $edit = array();
81
    // Add a display and override the pager settings.
82
    $this->drupalPost('admin/structure/views/view/test_store_pager_settings/edit', $edit, t('Add Page'));
83
    $edit = array(
84
      'override[dropdown]' => 'page_1',
85
    );
86
    $this->drupalPost('admin/structure/views/nojs/display/test_store_pager_settings/page_1/pager', $edit, t('Apply'));
87

    
88
    $edit = array(
89
      'pager[type]' => 'mini',
90
    );
91
    $this->drupalPost('admin/structure/views/nojs/display/test_store_pager_settings/page_1/pager', $edit, t('Apply'));
92
    $this->drupalGet('admin/structure/views/view/test_store_pager_settings/edit');
93
    $this->assertText('Mini', 'Changed pager plugin, should change some text');
94

    
95
    $edit = array(
96
      'pager_options[items_per_page]' => 10,
97
    );
98
    $this->drupalPost('admin/structure/views/nojs/display/test_store_pager_settings/default/pager_options', $edit, t('Apply'));
99
    $this->assertText('20 items');
100
  }
101

    
102
  /**
103
   *
104
   */
105
  public function viewsStorePagerSettings() {
106
    $view = new view();
107
    $view->name = 'test_store_pager_settings';
108
    $view->description = '';
109
    $view->tag = '';
110
    $view->view_php = '';
111
    $view->base_table = 'node';
112
    $view->is_cacheable = FALSE;
113
    $view->api_version = 3;
114
    $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
115

    
116
    /* Display: Master */
117
    $handler = $view->new_display('default', 'Master', 'default');
118
    $handler->display->display_options['access']['type'] = 'none';
119
    $handler->display->display_options['cache']['type'] = 'none';
120
    $handler->display->display_options['exposed_form']['type'] = 'basic';
121
    $handler->display->display_options['pager']['type'] = 'none';
122
    $handler->display->display_options['style_plugin'] = 'default';
123
    $handler->display->display_options['row_plugin'] = 'node';
124
    return $view;
125
  }
126

    
127
  /**
128
   * Tests the none-pager-query.
129
   */
130
  public function testNoLimit() {
131
    // Create 11 nodes and make sure that everyone is returned. We create 11
132
    // nodes, because the default pager plugin had 10 items per page.
133
    for ($i = 0; $i < 11; $i++) {
134
      $this->drupalCreateNode();
135
    }
136
    $view = $this->viewsPagerNoLimit();
137
    $view->set_display('default');
138
    $this->executeView($view);
139
    $this->assertEqual(count($view->result), 11, 'Make sure that every item is returned in the result');
140

    
141
    $view->destroy();
142

    
143
    // Setup and test a offset.
144
    $view = $this->viewsPagerNoLimit();
145
    $view->set_display('default');
146

    
147
    $pager = array(
148
      'type' => 'none',
149
      'options' => array(
150
        'offset' => 3,
151
      ),
152
    );
153
    $view->display_handler->set_option('pager', $pager);
154
    $this->executeView($view);
155

    
156
    $this->assertEqual(count($view->result), 8, 'Make sure that every item beside the first three is returned in the result');
157

    
158
    // Check some public functions.
159
    $this->assertFalse($view->query->pager->use_pager());
160
    $this->assertFalse($view->query->pager->use_count_query());
161
    $this->assertEqual($view->query->pager->get_items_per_page(), 0);
162
  }
163

    
164
  /**
165
   *
166
   */
167
  public function viewsPagerNoLimit() {
168
    $view = new view();
169
    $view->name = 'test_pager_none';
170
    $view->description = '';
171
    $view->tag = '';
172
    $view->view_php = '';
173
    $view->base_table = 'node';
174
    $view->is_cacheable = FALSE;
175
    $view->api_version = 3;
176
    $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
177

    
178
    /* Display: Master */
179
    $handler = $view->new_display('default', 'Master', 'default');
180
    $handler->display->display_options['access']['type'] = 'none';
181
    $handler->display->display_options['cache']['type'] = 'none';
182
    $handler->display->display_options['exposed_form']['type'] = 'basic';
183
    $handler->display->display_options['pager']['type'] = 'none';
184
    $handler->display->display_options['style_plugin'] = 'default';
185
    $handler->display->display_options['row_plugin'] = 'node';
186
    return $view;
187
  }
188

    
189
  /**
190
   *
191
   */
192
  public function testViewTotalRowsWithoutPager() {
193
    $this->createNodes(23);
194

    
195
    $view = $this->viewsPagerNoLimit();
196
    $view->get_total_rows = TRUE;
197
    $view->set_display('default');
198
    $this->executeView($view);
199

    
200
    $this->assertEqual($view->total_rows, 23, "'total_rows' is calculated when pager type is 'none' and 'get_total_rows' is TRUE.");
201
  }
202

    
203
  /**
204
   *
205
   */
206
  public function createNodes($count) {
207
    if ($count >= 0) {
208
      for ($i = 0; $i < $count; $i++) {
209
        $this->drupalCreateNode();
210
      }
211
    }
212
  }
213

    
214
  /**
215
   * Tests the some pager plugin.
216
   */
217
  public function testLimit() {
218
    // Create 11 nodes and make sure that everyone is returned. We create 11
219
    // nodes, because the default pager plugin had 10 items per page.
220
    for ($i = 0; $i < 11; $i++) {
221
      $this->drupalCreateNode();
222
    }
223
    $view = $this->viewsPagerLimit();
224
    $view->set_display('default');
225
    $this->executeView($view);
226
    $this->assertEqual(count($view->result), 5, 'Make sure that only a certain count of items is returned');
227
    $view->destroy();
228

    
229
    // Setup and test a offset.
230
    $view = $this->viewsPagerLimit();
231
    $view->set_display('default');
232

    
233
    $pager = array(
234
      'type' => 'none',
235
      'options' => array(
236
        'offset' => 8,
237
        'items_per_page' => 5,
238
      ),
239
    );
240
    $view->display_handler->set_option('pager', $pager);
241
    $this->executeView($view);
242
    $this->assertEqual(count($view->result), 3, 'Make sure that only a certain count of items is returned');
243

    
244
    // Check some public functions.
245
    $this->assertFalse($view->query->pager->use_pager());
246
    $this->assertFalse($view->query->pager->use_count_query());
247
  }
248

    
249
  /**
250
   *
251
   */
252
  public function viewsPagerLimit() {
253
    $view = new view();
254
    $view->name = 'test_pager_some';
255
    $view->description = '';
256
    $view->tag = '';
257
    $view->view_php = '';
258
    $view->base_table = 'node';
259
    $view->is_cacheable = FALSE;
260
    $view->api_version = 3;
261
    $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
262

    
263
    /* Display: Master */
264
    $handler = $view->new_display('default', 'Master', 'default');
265
    $handler->display->display_options['access']['type'] = 'none';
266
    $handler->display->display_options['cache']['type'] = 'none';
267
    $handler->display->display_options['exposed_form']['type'] = 'basic';
268
    $handler->display->display_options['pager']['type'] = 'some';
269
    $handler->display->display_options['pager']['options']['offset'] = 0;
270
    $handler->display->display_options['pager']['options']['items_per_page'] = 5;
271
    $handler->display->display_options['style_plugin'] = 'default';
272
    $handler->display->display_options['row_plugin'] = 'node';
273
    return $view;
274
  }
275

    
276
  /**
277
   * Tests the normal pager.
278
   */
279
  public function testNormalPager() {
280
    // Create 11 nodes and make sure that everyone is returned. We create 11
281
    // nodes, because the default pager plugin had 10 items per page.
282
    for ($i = 0; $i < 11; $i++) {
283
      $this->drupalCreateNode();
284
    }
285
    $view = $this->viewsPagerFull();
286
    $view->set_display('default');
287
    $this->executeView($view);
288
    $this->assertEqual(count($view->result), 5, 'Make sure that only a certain count of items is returned');
289
    $view->destroy();
290

    
291
    // Setup and test a offset.
292
    $view = $this->viewsPagerFull();
293
    $view->set_display('default');
294

    
295
    $pager = array(
296
      'type' => 'full',
297
      'options' => array(
298
        'offset' => 8,
299
        'items_per_page' => 5,
300
      ),
301
    );
302
    $view->display_handler->set_option('pager', $pager);
303
    $this->executeView($view);
304
    $this->assertEqual(count($view->result), 3, 'Make sure that only a certain count of items is returned');
305

    
306
    // Test items per page = 0
307
    $view = $this->viewPagerFullZeroItemsPerPage();
308
    $view->set_display('default');
309
    $this->executeView($view);
310

    
311
    $this->assertEqual(count($view->result), 11, 'All items are return');
312

    
313
    // @todo test number of pages.
314
    // Test items per page = 0.
315
    $view->destroy();
316

    
317
    // Setup and test a offset.
318
    $view = $this->viewsPagerFull();
319
    $view->set_display('default');
320

    
321
    $pager = array(
322
      'type' => 'full',
323
      'options' => array(
324
        'offset' => 0,
325
        'items_per_page' => 0,
326
      ),
327
    );
328

    
329
    $view->display_handler->set_option('pager', $pager);
330
    $this->executeView($view);
331
    $this->assertEqual($view->query->pager->get_items_per_page(), 0);
332
    $this->assertEqual(count($view->result), 11);
333
  }
334

    
335
  /**
336
   *
337
   */
338
  function viewPagerFullZeroItemsPerPage() {
339
    $view = new view();
340
    $view->name = 'view_pager_full_zero_items_per_page';
341
    $view->description = '';
342
    $view->tag = '';
343
    $view->view_php = '';
344
    $view->base_table = 'node';
345
    $view->is_cacheable = FALSE;
346
    $view->api_version = 3;
347
    $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
348

    
349
    /* Display: Master */
350
    $handler = $view->new_display('default', 'Master', 'default');
351
    $handler->display->display_options['access']['type'] = 'none';
352
    $handler->display->display_options['cache']['type'] = 'none';
353
    $handler->display->display_options['exposed_form']['type'] = 'basic';
354
    $handler->display->display_options['pager']['type'] = 'full';
355
    $handler->display->display_options['pager']['options']['items_per_page'] = '0';
356
    $handler->display->display_options['pager']['options']['offset'] = '0';
357
    $handler->display->display_options['pager']['options']['id'] = '0';
358
    $handler->display->display_options['style_plugin'] = 'default';
359
    $handler->display->display_options['row_plugin'] = 'fields';
360
    /* Field: Content: Title */
361
    $handler->display->display_options['fields']['title']['id'] = 'title';
362
    $handler->display->display_options['fields']['title']['table'] = 'node';
363
    $handler->display->display_options['fields']['title']['field'] = 'title';
364
    $handler->display->display_options['fields']['title']['alter']['alter_text'] = 0;
365
    $handler->display->display_options['fields']['title']['alter']['make_link'] = 0;
366
    $handler->display->display_options['fields']['title']['alter']['trim'] = 0;
367
    $handler->display->display_options['fields']['title']['alter']['word_boundary'] = 1;
368
    $handler->display->display_options['fields']['title']['alter']['ellipsis'] = 1;
369
    $handler->display->display_options['fields']['title']['alter']['strip_tags'] = 0;
370
    $handler->display->display_options['fields']['title']['alter']['html'] = 0;
371
    $handler->display->display_options['fields']['title']['hide_empty'] = 0;
372
    $handler->display->display_options['fields']['title']['empty_zero'] = 0;
373
    $handler->display->display_options['fields']['title']['link_to_node'] = 0;
374

    
375
    return $view;
376
  }
377

    
378
  /**
379
   *
380
   */
381
  function viewsPagerFull() {
382
    $view = new view();
383
    $view->name = 'test_pager_full';
384
    $view->description = '';
385
    $view->tag = '';
386
    $view->view_php = '';
387
    $view->base_table = 'node';
388
    $view->is_cacheable = FALSE;
389
    $view->api_version = 3;
390
    $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
391

    
392
    /* Display: Master */
393
    $handler = $view->new_display('default', 'Master', 'default');
394
    $handler->display->display_options['access']['type'] = 'none';
395
    $handler->display->display_options['cache']['type'] = 'none';
396
    $handler->display->display_options['exposed_form']['type'] = 'basic';
397
    $handler->display->display_options['pager']['type'] = 'full';
398
    $handler->display->display_options['pager']['options']['items_per_page'] = '5';
399
    $handler->display->display_options['pager']['options']['offset'] = '0';
400
    $handler->display->display_options['pager']['options']['id'] = '0';
401
    $handler->display->display_options['style_plugin'] = 'default';
402
    $handler->display->display_options['row_plugin'] = 'node';
403

    
404
    return $view;
405
  }
406

    
407
  /**
408
   *
409
   */
410
  function viewsPagerFullFields() {
411
    $view = new view();
412
    $view->name = 'test_pager_full';
413
    $view->description = '';
414
    $view->tag = '';
415
    $view->view_php = '';
416
    $view->base_table = 'node';
417
    $view->is_cacheable = FALSE;
418
    $view->api_version = 3;
419
    $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
420

    
421
    /* Display: Master */
422
    $handler = $view->new_display('default', 'Master', 'default');
423
    $handler->display->display_options['access']['type'] = 'none';
424
    $handler->display->display_options['cache']['type'] = 'none';
425
    $handler->display->display_options['exposed_form']['type'] = 'basic';
426
    $handler->display->display_options['pager']['type'] = 'full';
427
    $handler->display->display_options['pager']['options']['items_per_page'] = '5';
428
    $handler->display->display_options['pager']['options']['offset'] = '0';
429
    $handler->display->display_options['pager']['options']['id'] = '0';
430
    $handler->display->display_options['style_plugin'] = 'default';
431
    $handler->display->display_options['row_plugin'] = 'fields';
432
    $handler->display->display_options['fields']['title']['id'] = 'title';
433
    $handler->display->display_options['fields']['title']['table'] = 'node';
434
    $handler->display->display_options['fields']['title']['field'] = 'title';
435
    $handler->display->display_options['fields']['title']['alter']['alter_text'] = 0;
436
    $handler->display->display_options['fields']['title']['alter']['make_link'] = 0;
437
    $handler->display->display_options['fields']['title']['alter']['trim'] = 0;
438
    $handler->display->display_options['fields']['title']['alter']['word_boundary'] = 1;
439
    $handler->display->display_options['fields']['title']['alter']['ellipsis'] = 1;
440
    $handler->display->display_options['fields']['title']['alter']['strip_tags'] = 0;
441
    $handler->display->display_options['fields']['title']['alter']['html'] = 0;
442
    $handler->display->display_options['fields']['title']['hide_empty'] = 0;
443
    $handler->display->display_options['fields']['title']['empty_zero'] = 0;
444
    $handler->display->display_options['fields']['title']['link_to_node'] = 0;
445
    return $view;
446
  }
447

    
448
  /**
449
   * Tests the minipager.
450
   */
451
  public function testMiniPager() {
452
    // The functionality is the same as the normal pager, so i don't know what
453
    // to test here.
454
  }
455

    
456
  /**
457
   * Tests rendering with NULL pager.
458
   */
459
  public function testRenderNullPager() {
460
    // Create 11 nodes and make sure that everyone is returned. We create 11
461
    // nodes, because the default pager plugin had 10 items per page.
462
    for ($i = 0; $i < 11; $i++) {
463
      $this->drupalCreateNode();
464
    }
465
    $view = $this->viewsPagerFullFields();
466
    $view->set_display('default');
467
    $this->executeView($view);
468
    $view->use_ajax = TRUE;
469
    // force the value again here.
470
    $view->query->pager = NULL;
471
    $output = $view->render();
472
    $this->assertEqual(preg_match('/<ul class="pager">/', $output), 0, t('The pager is not rendered.'));
473
  }
474

    
475
  /**
476
   * Test the api functions on the view object.
477
   */
478
  function testPagerApi() {
479
    $view = $this->viewsPagerFull();
480
    // On the first round don't initialize the pager.
481
    $this->assertEqual($view->get_items_per_page(), NULL, 'If the pager is not initialized and no manual override there is no items per page.');
482
    $rand_number = rand(1, 5);
483
    $view->set_items_per_page($rand_number);
484
    $this->assertEqual($view->get_items_per_page(), $rand_number, 'Make sure get_items_per_page uses the settings of set_items_per_page.');
485

    
486
    $this->assertEqual($view->get_offset(), NULL, 'If the pager is not initialized and no manual override there is no offset.');
487
    $rand_number = rand(1, 5);
488
    $view->set_offset($rand_number);
489
    $this->assertEqual($view->get_offset(), $rand_number, 'Make sure get_offset uses the settings of set_offset.');
490

    
491
    $this->assertEqual($view->get_current_page(), NULL, 'If the pager is not initialized and no manual override there is no current page.');
492
    $rand_number = rand(1, 5);
493
    $view->set_current_page($rand_number);
494
    $this->assertEqual($view->get_current_page(), $rand_number, 'Make sure get_current_page uses the settings of set_current_page.');
495

    
496
    $view->destroy();
497

    
498
    // On this round enable the pager.
499
    $view->init_display();
500
    $view->init_query();
501
    $view->init_pager();
502

    
503
    $this->assertEqual($view->get_items_per_page(), 5, 'Per default the view has 5 items per page.');
504
    $rand_number = rand(1, 5);
505
    $view->set_items_per_page($rand_number);
506
    $rand_number = rand(6, 11);
507
    $view->query->pager->set_items_per_page($rand_number);
508
    $this->assertEqual($view->get_items_per_page(), $rand_number, 'Make sure get_items_per_page uses the settings of set_items_per_page.');
509

    
510
    $this->assertEqual($view->get_offset(), 0, 'Per default a view has a 0 offset.');
511
    $rand_number = rand(1, 5);
512
    $view->set_offset($rand_number);
513
    $rand_number = rand(6, 11);
514
    $view->query->pager->set_offset($rand_number);
515
    $this->assertEqual($view->get_offset(), $rand_number, 'Make sure get_offset uses the settings of set_offset.');
516

    
517
    $this->assertEqual($view->get_current_page(), 0, 'Per default the current page is 0.');
518
    $rand_number = rand(1, 5);
519
    $view->set_current_page($rand_number);
520
    $rand_number = rand(6, 11);
521
    $view->query->pager->set_current_page($rand_number);
522
    $this->assertEqual($view->get_current_page(), $rand_number, 'Make sure get_current_page uses the settings of set_current_page.');
523
  }
524

    
525
}