PaginatorTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.5.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Datasource;
  16. use Cake\Core\Configure;
  17. use Cake\Datasource\ConnectionManager;
  18. use Cake\Datasource\EntityInterface;
  19. use Cake\Datasource\Exception\PageOutOfBoundsException;
  20. use Cake\Datasource\Paginator;
  21. use Cake\ORM\Entity;
  22. use Cake\TestSuite\TestCase;
  23. class PaginatorTest extends TestCase
  24. {
  25. use PaginatorTestTrait;
  26. /**
  27. * Don't load data for fixtures for all tests
  28. *
  29. * @var bool
  30. */
  31. public $autoFixtures = false;
  32. /**
  33. * test paginate() and custom find, to make sure the correct count is returned.
  34. *
  35. * @return void
  36. */
  37. public function testPaginateCustomFind()
  38. {
  39. $this->loadFixtures('Posts');
  40. $titleExtractor = function ($result) {
  41. $ids = [];
  42. foreach ($result as $record) {
  43. $ids[] = $record->title;
  44. }
  45. return $ids;
  46. };
  47. $table = $this->getTableLocator()->get('PaginatorPosts');
  48. $data = ['author_id' => 3, 'title' => 'Fourth Post', 'body' => 'Article Body, unpublished', 'published' => 'N'];
  49. $result = $table->save(new Entity($data));
  50. $this->assertNotEmpty($result);
  51. $result = $this->Paginator->paginate($table);
  52. $this->assertCount(4, $result, '4 rows should come back');
  53. $this->assertEquals(['First Post', 'Second Post', 'Third Post', 'Fourth Post'], $titleExtractor($result));
  54. $pagingParams = $this->Paginator->getPagingParams();
  55. $this->assertEquals(4, $pagingParams['PaginatorPosts']['current']);
  56. $this->assertEquals(4, $pagingParams['PaginatorPosts']['count']);
  57. $settings = ['finder' => 'published'];
  58. $result = $this->Paginator->paginate($table, [], $settings);
  59. $this->assertCount(3, $result, '3 rows should come back');
  60. $this->assertEquals(['First Post', 'Second Post', 'Third Post'], $titleExtractor($result));
  61. $pagingParams = $this->Paginator->getPagingParams();
  62. $this->assertEquals(3, $pagingParams['PaginatorPosts']['current']);
  63. $this->assertEquals(3, $pagingParams['PaginatorPosts']['count']);
  64. $settings = ['finder' => 'published', 'limit' => 2, 'page' => 2];
  65. $result = $this->Paginator->paginate($table, [], $settings);
  66. $this->assertCount(1, $result, '1 rows should come back');
  67. $this->assertEquals(['Third Post'], $titleExtractor($result));
  68. $pagingParams = $this->Paginator->getPagingParams();
  69. $this->assertEquals(1, $pagingParams['PaginatorPosts']['current']);
  70. $this->assertEquals(3, $pagingParams['PaginatorPosts']['count']);
  71. $this->assertEquals(2, $pagingParams['PaginatorPosts']['pageCount']);
  72. $settings = ['finder' => 'published', 'limit' => 2];
  73. $result = $this->Paginator->paginate($table, [], $settings);
  74. $this->assertCount(2, $result, '2 rows should come back');
  75. $this->assertEquals(['First Post', 'Second Post'], $titleExtractor($result));
  76. $pagingParams = $this->Paginator->getPagingParams();
  77. $this->assertEquals(2, $pagingParams['PaginatorPosts']['current']);
  78. $this->assertEquals(3, $pagingParams['PaginatorPosts']['count']);
  79. $this->assertEquals(2, $pagingParams['PaginatorPosts']['pageCount']);
  80. $this->assertTrue($pagingParams['PaginatorPosts']['nextPage']);
  81. $this->assertFalse($pagingParams['PaginatorPosts']['prevPage']);
  82. $this->assertEquals(2, $pagingParams['PaginatorPosts']['perPage']);
  83. $this->assertNull($pagingParams['PaginatorPosts']['limit']);
  84. }
  85. /**
  86. * test paginate() and custom find with fields array, to make sure the correct count is returned.
  87. *
  88. * @return void
  89. */
  90. public function testPaginateCustomFindFieldsArray()
  91. {
  92. $this->loadFixtures('Posts');
  93. $table = $this->getTableLocator()->get('PaginatorPosts');
  94. $data = ['author_id' => 3, 'title' => 'Fourth Article', 'body' => 'Article Body, unpublished', 'published' => 'N'];
  95. $table->save(new Entity($data));
  96. $settings = [
  97. 'finder' => 'list',
  98. 'conditions' => ['PaginatorPosts.published' => 'Y'],
  99. 'limit' => 2
  100. ];
  101. $results = $this->Paginator->paginate($table, [], $settings);
  102. $result = $results->toArray();
  103. $expected = [
  104. 1 => 'First Post',
  105. 2 => 'Second Post',
  106. ];
  107. $this->assertEquals($expected, $result);
  108. $result = $this->Paginator->getPagingParams()['PaginatorPosts'];
  109. $this->assertEquals(2, $result['current']);
  110. $this->assertEquals(3, $result['count']);
  111. $this->assertEquals(2, $result['pageCount']);
  112. $this->assertTrue($result['nextPage']);
  113. $this->assertFalse($result['prevPage']);
  114. }
  115. /**
  116. * Test that special paginate types are called and that the type param doesn't leak out into defaults or options.
  117. *
  118. * @return void
  119. */
  120. public function testPaginateCustomFinder()
  121. {
  122. $settings = [
  123. 'PaginatorPosts' => [
  124. 'finder' => 'published',
  125. 'fields' => ['id', 'title'],
  126. 'maxLimit' => 10,
  127. ]
  128. ];
  129. $this->loadFixtures('Posts');
  130. $table = $this->getTableLocator()->get('PaginatorPosts');
  131. $table->updateAll(['published' => 'N'], ['id' => 2]);
  132. $this->Paginator->paginate($table, [], $settings);
  133. $pagingParams = $this->Paginator->getPagingParams();
  134. $this->assertSame('published', $pagingParams['PaginatorPosts']['finder']);
  135. $this->assertSame(1, $pagingParams['PaginatorPosts']['start']);
  136. $this->assertSame(2, $pagingParams['PaginatorPosts']['end']);
  137. $this->assertFalse($pagingParams['PaginatorPosts']['nextPage']);
  138. }
  139. }