PaginatorTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. * fixtures property
  28. *
  29. * @var array
  30. */
  31. public $fixtures = [
  32. 'core.Posts', 'core.Articles', 'core.ArticlesTags',
  33. 'core.Authors', 'core.AuthorsTags', 'core.Tags',
  34. ];
  35. /**
  36. * Don't load data for fixtures for all tests
  37. *
  38. * @var bool
  39. */
  40. public $autoFixtures = false;
  41. /**
  42. * test paginate() and custom find, to make sure the correct count is returned.
  43. *
  44. * @return void
  45. */
  46. public function testPaginateCustomFind()
  47. {
  48. $this->loadFixtures('Posts');
  49. $titleExtractor = function ($result) {
  50. $ids = [];
  51. foreach ($result as $record) {
  52. $ids[] = $record->title;
  53. }
  54. return $ids;
  55. };
  56. $table = $this->getTableLocator()->get('PaginatorPosts');
  57. $data = ['author_id' => 3, 'title' => 'Fourth Post', 'body' => 'Article Body, unpublished', 'published' => 'N'];
  58. $result = $table->save(new Entity($data));
  59. $this->assertNotEmpty($result);
  60. $result = $this->Paginator->paginate($table);
  61. $this->assertCount(4, $result, '4 rows should come back');
  62. $this->assertEquals(['First Post', 'Second Post', 'Third Post', 'Fourth Post'], $titleExtractor($result));
  63. $pagingParams = $this->Paginator->getPagingParams();
  64. $this->assertEquals(4, $pagingParams['PaginatorPosts']['current']);
  65. $this->assertEquals(4, $pagingParams['PaginatorPosts']['count']);
  66. $settings = ['finder' => 'published'];
  67. $result = $this->Paginator->paginate($table, [], $settings);
  68. $this->assertCount(3, $result, '3 rows should come back');
  69. $this->assertEquals(['First Post', 'Second Post', 'Third Post'], $titleExtractor($result));
  70. $pagingParams = $this->Paginator->getPagingParams();
  71. $this->assertEquals(3, $pagingParams['PaginatorPosts']['current']);
  72. $this->assertEquals(3, $pagingParams['PaginatorPosts']['count']);
  73. $settings = ['finder' => 'published', 'limit' => 2, 'page' => 2];
  74. $result = $this->Paginator->paginate($table, [], $settings);
  75. $this->assertCount(1, $result, '1 rows should come back');
  76. $this->assertEquals(['Third Post'], $titleExtractor($result));
  77. $pagingParams = $this->Paginator->getPagingParams();
  78. $this->assertEquals(1, $pagingParams['PaginatorPosts']['current']);
  79. $this->assertEquals(3, $pagingParams['PaginatorPosts']['count']);
  80. $this->assertEquals(2, $pagingParams['PaginatorPosts']['pageCount']);
  81. $settings = ['finder' => 'published', 'limit' => 2];
  82. $result = $this->Paginator->paginate($table, [], $settings);
  83. $this->assertCount(2, $result, '2 rows should come back');
  84. $this->assertEquals(['First Post', 'Second Post'], $titleExtractor($result));
  85. $pagingParams = $this->Paginator->getPagingParams();
  86. $this->assertEquals(2, $pagingParams['PaginatorPosts']['current']);
  87. $this->assertEquals(3, $pagingParams['PaginatorPosts']['count']);
  88. $this->assertEquals(2, $pagingParams['PaginatorPosts']['pageCount']);
  89. $this->assertTrue($pagingParams['PaginatorPosts']['nextPage']);
  90. $this->assertFalse($pagingParams['PaginatorPosts']['prevPage']);
  91. $this->assertEquals(2, $pagingParams['PaginatorPosts']['perPage']);
  92. $this->assertNull($pagingParams['PaginatorPosts']['limit']);
  93. }
  94. /**
  95. * test paginate() and custom find with fields array, to make sure the correct count is returned.
  96. *
  97. * @return void
  98. */
  99. public function testPaginateCustomFindFieldsArray()
  100. {
  101. $this->loadFixtures('Posts');
  102. $table = $this->getTableLocator()->get('PaginatorPosts');
  103. $data = ['author_id' => 3, 'title' => 'Fourth Article', 'body' => 'Article Body, unpublished', 'published' => 'N'];
  104. $table->save(new Entity($data));
  105. $settings = [
  106. 'finder' => 'list',
  107. 'conditions' => ['PaginatorPosts.published' => 'Y'],
  108. 'limit' => 2,
  109. ];
  110. $results = $this->Paginator->paginate($table, [], $settings);
  111. $result = $results->toArray();
  112. $expected = [
  113. 1 => 'First Post',
  114. 2 => 'Second Post',
  115. ];
  116. $this->assertEquals($expected, $result);
  117. $result = $this->Paginator->getPagingParams()['PaginatorPosts'];
  118. $this->assertEquals(2, $result['current']);
  119. $this->assertEquals(3, $result['count']);
  120. $this->assertEquals(2, $result['pageCount']);
  121. $this->assertTrue($result['nextPage']);
  122. $this->assertFalse($result['prevPage']);
  123. }
  124. /**
  125. * Test that special paginate types are called and that the type param doesn't leak out into defaults or options.
  126. *
  127. * @return void
  128. */
  129. public function testPaginateCustomFinder()
  130. {
  131. $settings = [
  132. 'PaginatorPosts' => [
  133. 'finder' => 'published',
  134. 'fields' => ['id', 'title'],
  135. 'maxLimit' => 10,
  136. ],
  137. ];
  138. $this->loadFixtures('Posts');
  139. $table = $this->getTableLocator()->get('PaginatorPosts');
  140. $table->updateAll(['published' => 'N'], ['id' => 2]);
  141. $this->Paginator->paginate($table, [], $settings);
  142. $pagingParams = $this->Paginator->getPagingParams();
  143. $this->assertSame('published', $pagingParams['PaginatorPosts']['finder']);
  144. $this->assertSame(1, $pagingParams['PaginatorPosts']['start']);
  145. $this->assertSame(2, $pagingParams['PaginatorPosts']['end']);
  146. $this->assertFalse($pagingParams['PaginatorPosts']['nextPage']);
  147. }
  148. }