SimplePaginatorTest.php 6.2 KB

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