SimplePaginatorTest.php 6.3 KB

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