SimplePaginatorTest.php 6.2 KB

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