123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495 |
- <?php
- /**
- * Part of the Fuel framework.
- *
- * @package Fuel
- * @version 1.5
- * @author Fuel Development Team
- * @license MIT License
- * @copyright 2010 - 2013 Fuel Development Team
- * @link http://fuelphp.com
- */
- namespace Fuel\Core;
- /**
- * Pagination class tests
- *
- * @group Core
- * @group Pagination
- */
- class Test_Pagination extends TestCase
- {
- protected function set_request($uri)
- {
- // set Request::$main
- $this->request = \Request::forge($uri);
- $rp = new \ReflectionProperty($this->request, 'main');
- $rp->setAccessible(true);
- $rp->setValue($this->request, $this->request);
- // set Request::$active
- $rp = new \ReflectionProperty($this->request, 'active');
- $rp->setAccessible(true);
- $rp->setValue($this->request, $this->request);
- }
- public function tearDown()
- {
- // reset Request::$main
- $request = \Request::forge();
- $rp = new \ReflectionProperty($request, 'main');
- $rp->setAccessible(true);
- $rp->setValue($request, false);
- // reset Request::$active
- $rp = new \ReflectionProperty($request, 'active');
- $rp->setAccessible(true);
- $rp->setValue($request, false);
- }
- /**********************************
- * Tests for URI Segment Pagination
- **********************************/
- protected function set_uri_segment_config()
- {
- $this->config = array(
- 'uri_segment' => 3,
- 'pagination_url' => 'http://docs.fuelphp.com/welcome/index/',
- 'total_items' => 100,
- 'per_page' => 10,
- );
- }
- public function test_uri_segment_auto_detect_pagination_url()
- {
- // set base_url
- Config::set('base_url', 'http://docs.fuelphp.com/');
- // set Request::$main & $active
- $this->set_request('welcome/index/5');
- $this->set_uri_segment_config();
- $this->config['pagination_url'] = null;
- $pagination = Pagination::forge(__METHOD__, $this->config);
- // set _make_link() accessible
- $_make_link = new \ReflectionMethod($pagination, '_make_link');
- $_make_link->setAccessible(true);
- $test = $_make_link->invoke($pagination, 1);
- $expected = 'http://docs.fuelphp.com/welcome/index/1';
- $this->assertEquals($expected, $test);
- // reset base_url
- Config::set('base_url', null);
- }
- public function test_uri_segment_set_pagination_url_after_forging_fail()
- {
- // set Request::$main & $active
- $this->set_request('welcome/index/3');
- $this->set_uri_segment_config();
- $pagination = Pagination::forge(__METHOD__, $this->config);
- $pagination->pagination_url = 'http://example.com/page/';
- // set _make_link() accessible
- $_make_link = new \ReflectionMethod($pagination, '_make_link');
- $_make_link->setAccessible(true);
- // not enough segments in the URI to add the page number
- $this->setExpectedException('RunTimeException');
- $test = $_make_link->invoke($pagination, 1);
- }
- public function test_uri_segment_set_pagination_url_after_forging_success()
- {
- // set Request::$main & $active
- $this->set_request('welcome/index/3');
- $this->set_uri_segment_config();
- $pagination = Pagination::forge(__METHOD__, $this->config);
- $pagination->pagination_url = 'http://example.com/this/page/';
- // set _make_link() accessible
- $_make_link = new \ReflectionMethod($pagination, '_make_link');
- $_make_link->setAccessible(true);
- $test = $_make_link->invoke($pagination, 1);
- $expected = 'http://example.com/this/page/1';
- $this->assertEquals($expected, $test);
- }
- public function test_uri_segment_get_total_pages()
- {
- // set Request::$main & $active
- $this->set_request('welcome/index/');
- $this->set_uri_segment_config();
- $pagination = Pagination::forge(__METHOD__, $this->config);
- $test = $pagination->total_pages;
- $expected = 10;
- $this->assertEquals($expected, $test);
- }
- /**
- * first page
- *
- */
- public function test_uri_segment_first_page()
- {
- // set Request::$main & $active
- $this->set_request('welcome/index/');
- $this->set_uri_segment_config();
- $pagination = Pagination::forge(__METHOD__, $this->config);
- $output = $pagination->previous();
- $output = str_replace(array("\n", "\t"), "", $output);
- $expected = '<span class="previous-inactive"><a href="#">«</a></span>';
- $this->assertEquals($expected, $output);
- $output = $pagination->pages_render();
- $output = str_replace(array("\n", "\t"), "", $output);
- $expected = '<span class="active"><a href="#">1</a></span><span><a href="http://docs.fuelphp.com/welcome/index/2">2</a></span><span><a href="http://docs.fuelphp.com/welcome/index/3">3</a></span><span><a href="http://docs.fuelphp.com/welcome/index/4">4</a></span><span><a href="http://docs.fuelphp.com/welcome/index/5">5</a></span><span><a href="http://docs.fuelphp.com/welcome/index/6">6</a></span>';
- $this->assertEquals($expected, $output);
- $output = $pagination->next();
- $output = str_replace(array("\n", "\t"), "", $output);
- $expected = '<span class="next"><a href="http://docs.fuelphp.com/welcome/index/2">»</a></span>';
- $this->assertEquals($expected, $output);
- $output = $pagination->render();
- $output = str_replace(array("\n", "\t"), "", $output);
- $expected = '<div class="pagination"><span class="previous-inactive"><a href="#">«</a></span><span class="active"><a href="#">1</a></span><span><a href="http://docs.fuelphp.com/welcome/index/2">2</a></span><span><a href="http://docs.fuelphp.com/welcome/index/3">3</a></span><span><a href="http://docs.fuelphp.com/welcome/index/4">4</a></span><span><a href="http://docs.fuelphp.com/welcome/index/5">5</a></span><span><a href="http://docs.fuelphp.com/welcome/index/6">6</a></span><span class="next"><a href="http://docs.fuelphp.com/welcome/index/2">»</a></span></div>';
- $this->assertEquals($expected, $output);
- }
- /**
- * last page
- *
- */
- public function test_uri_segment_nextlink_inactive()
- {
- // set Request::$main & $active
- $this->set_request('welcome/index/10');
- $this->set_uri_segment_config();
- $pagination = Pagination::forge(__METHOD__, $this->config);
- $output = $pagination->next();
- $output = str_replace(array("\n", "\t"), "", $output);
- $expected = '<span class="next-inactive"><a href="#">»</a></span>';
- $this->assertEquals($expected, $output);
- $output = $pagination->pages_render();
- $output = str_replace(array("\n", "\t"), "", $output);
- $expected = '<span><a href="http://docs.fuelphp.com/welcome/index/6">6</a></span><span><a href="http://docs.fuelphp.com/welcome/index/7">7</a></span><span><a href="http://docs.fuelphp.com/welcome/index/8">8</a></span><span><a href="http://docs.fuelphp.com/welcome/index/9">9</a></span><span class="active"><a href="#">10</a></span>';
- $this->assertEquals($expected, $output);
- $output = $pagination->previous();
- $output = str_replace(array("\n", "\t"), "", $output);
- $expected = '<span class="previous"><a href="http://docs.fuelphp.com/welcome/index/9">«</a></span>';
- $this->assertEquals($expected, $output);
- $output = $pagination->render();
- $output = str_replace(array("\n", "\t"), "", $output);
- $expected = '<div class="pagination"><span class="previous"><a href="http://docs.fuelphp.com/welcome/index/9">«</a></span><span><a href="http://docs.fuelphp.com/welcome/index/6">6</a></span><span><a href="http://docs.fuelphp.com/welcome/index/7">7</a></span><span><a href="http://docs.fuelphp.com/welcome/index/8">8</a></span><span><a href="http://docs.fuelphp.com/welcome/index/9">9</a></span><span class="active"><a href="#">10</a></span><span class="next-inactive"><a href="#">»</a></span></div>';
- $this->assertEquals($expected, $output);
- }
- /**
- * total page is 1
- *
- */
- public function test_uri_segment_total_page_is_one()
- {
- // set Request::$main & $active
- $this->set_request('welcome/index/10');
- $this->set_uri_segment_config();
- $this->config['per_page'] = 1000;
- $pagination = Pagination::forge(__METHOD__, $this->config);
- $output = $pagination->pages_render();
- $output = str_replace(array("\n", "\t"), "", $output);
- $expected = '';
- $this->assertEquals($expected, $output);
- $output = $pagination->render();
- $output = str_replace(array("\n", "\t"), "", $output);
- $expected = '';
- $this->assertEquals($expected, $output);
- }
- public function test_uri_segment_make_link_with_no_query_string_ending_page_number()
- {
- // set Request::$main & $active
- $this->set_request('welcome/index/10');
- $this->set_uri_segment_config();
- $this->config['pagination_url'] = 'http://docs.fuelphp.com/welcome/index/55';
- $pagination = Pagination::forge(__METHOD__, $this->config);
- // set _make_link() accessible
- $_make_link = new \ReflectionMethod($pagination, '_make_link');
- $_make_link->setAccessible(true);
- $test = $_make_link->invoke($pagination, 1);
- $expected = 'http://docs.fuelphp.com/welcome/index/1';
- $this->assertEquals($expected, $test);
- $test = $_make_link->invoke($pagination, 99);
- $expected = 'http://docs.fuelphp.com/welcome/index/99';
- $this->assertEquals($expected, $test);
- }
- public function test_uri_segment_make_link_with_no_query_string_ending_slash()
- {
- // set Request::$main & $active
- $this->set_request('welcome/index/');
- $this->set_uri_segment_config();
- $this->config['pagination_url'] = 'http://docs.fuelphp.com/welcome/index/';
- $pagination = Pagination::forge(__METHOD__, $this->config);
- // set _make_link() accessible
- $_make_link = new \ReflectionMethod($pagination, '_make_link');
- $_make_link->setAccessible(true);
- $test = $_make_link->invoke($pagination, 1);
- $expected = 'http://docs.fuelphp.com/welcome/index/1';
- $this->assertEquals($expected, $test);
- $test = $_make_link->invoke($pagination, 99);
- $expected = 'http://docs.fuelphp.com/welcome/index/99';
- $this->assertEquals($expected, $test);
- }
- public function test_uri_segment_make_link_with_query_string_ending_page_number()
- {
- // set Request::$main & $active
- $this->set_request('welcome/index/55?foo=bar&fuel[]=php1&fuel[]=php2&');
- $this->set_uri_segment_config();
- // no define pagination_url
- $this->config['pagination_url'] = null;
- $pagination = Pagination::forge(__METHOD__, $this->config);
- // set _make_link() accessible
- $_make_link = new \ReflectionMethod($pagination, '_make_link');
- $_make_link->setAccessible(true);
- $test = $_make_link->invoke($pagination, 1);
- $expected = '/welcome/index/1?foo=bar&fuel%5B0%5D=php1&fuel%5B1%5D=php2';
- $this->assertEquals($expected, $test);
- $test = $_make_link->invoke($pagination, 99);
- $expected = '/welcome/index/99?foo=bar&fuel%5B0%5D=php1&fuel%5B1%5D=php2';
- $this->assertEquals($expected, $test);
- }
- public function test_uri_segment_make_link_with_query_string_ending_slash()
- {
- // set Request::$main & $active
- $this->set_request('welcome/index/?foo=bar&fuel[]=php1&fuel[]=php2&');
- $this->set_uri_segment_config();
- // no define pagination_url
- $this->config['pagination_url'] = null;
- $pagination = Pagination::forge(__METHOD__, $this->config);
- // set _make_link() accessible
- $_make_link = new \ReflectionMethod($pagination, '_make_link');
- $_make_link->setAccessible(true);
- $test = $_make_link->invoke($pagination, 1);
- $expected = '/welcome/index/1?foo=bar&fuel%5B0%5D=php1&fuel%5B1%5D=php2';
- $this->assertEquals($expected, $test);
- $test = $_make_link->invoke($pagination, 99);
- $expected = '/welcome/index/99?foo=bar&fuel%5B0%5D=php1&fuel%5B1%5D=php2';
- $this->assertEquals($expected, $test);
- }
- /***********************************
- * Tests for Query String Pagination
- ***********************************/
- protected function set_query_string_config()
- {
- $this->config = array(
- 'uri_segment' => 'p',
- 'pagination_url' => 'http://docs.fuelphp.com/',
- 'total_items' => 100,
- 'per_page' => 10,
- );
- }
- public function test_query_string_auto_detect_pagination_url()
- {
- // set base_url
- Config::set('base_url', 'http://docs.fuelphp.com/');
- // set Request::$main & $active
- $this->set_request('/');
- $this->set_query_string_config();
- $this->config['pagination_url'] = null;
- $pagination = Pagination::forge(__METHOD__, $this->config);
- // set _make_link() accessible
- $_make_link = new \ReflectionMethod($pagination, '_make_link');
- $_make_link->setAccessible(true);
- $test = $_make_link->invoke($pagination, 1);
- $expected = 'http://docs.fuelphp.com/?p=1';
- $this->assertEquals($expected, $test);
- // reset base_url
- Config::set('base_url', null);
- }
- public function test_query_string_get_total_pages()
- {
- // set Request::$main & $active
- $this->set_request('/');
- $this->set_query_string_config();
- $pagination = Pagination::forge(__METHOD__, $this->config);
- $test = $pagination->total_pages;
- $expected = 10;
- $this->assertEquals($expected, $test);
- }
- /**
- * first page
- *
- */
- public function test_query_string_first_page()
- {
- // set Request::$main & $active
- $this->set_request('/');
- $this->set_query_string_config();
- $pagination = Pagination::forge(__METHOD__, $this->config);
- $pagination->current_page = 1;
- $output = $pagination->previous();
- $output = str_replace(array("\n", "\t"), "", $output);
- $expected = '<span class="previous-inactive"><a href="#">«</a></span>';
- $this->assertEquals($expected, $output);
- $output = $pagination->pages_render();
- $output = str_replace(array("\n", "\t"), "", $output);
- $expected = '<span class="active"><a href="#">1</a></span><span><a href="http://docs.fuelphp.com/?p=2">2</a></span><span><a href="http://docs.fuelphp.com/?p=3">3</a></span><span><a href="http://docs.fuelphp.com/?p=4">4</a></span><span><a href="http://docs.fuelphp.com/?p=5">5</a></span><span><a href="http://docs.fuelphp.com/?p=6">6</a></span>';
- $this->assertEquals($expected, $output);
- $output = $pagination->next();
- $output = str_replace(array("\n", "\t"), "", $output);
- $expected = '<span class="next"><a href="http://docs.fuelphp.com/?p=2">»</a></span>';
- $this->assertEquals($expected, $output);
- }
- /**
- * last page
- *
- */
- public function test_query_string_nextlink_inactive()
- {
- // set Request::$main & $active
- $this->set_request('/');
- $this->set_query_string_config();
- $pagination = Pagination::forge(__METHOD__, $this->config);
- $pagination->current_page = 10;
- $output = $pagination->next();
- $output = str_replace(array("\n", "\t"), "", $output);
- $expected = '<span class="next-inactive"><a href="#">»</a></span>';
- $this->assertEquals($expected, $output);
- $output = $pagination->pages_render();
- $output = str_replace(array("\n", "\t"), "", $output);
- $expected = '<span><a href="http://docs.fuelphp.com/?p=6">6</a></span><span><a href="http://docs.fuelphp.com/?p=7">7</a></span><span><a href="http://docs.fuelphp.com/?p=8">8</a></span><span><a href="http://docs.fuelphp.com/?p=9">9</a></span><span class="active"><a href="#">10</a></span>';
- $this->assertEquals($expected, $output);
- $output = $pagination->previous();
- $output = str_replace(array("\n", "\t"), "", $output);
- $expected = '<span class="previous"><a href="http://docs.fuelphp.com/?p=9">«</a></span>';
- $this->assertEquals($expected, $output);
- }
- public function test_query_string_make_link_by_request()
- {
- // set Request::$main & $active
- $this->set_request('welcome/index/?foo=bar&fuel[]=php1&fuel[]=php2&p=40');
- $this->set_query_string_config();
- $this->config['pagination_url'] = null;
- $pagination = Pagination::forge(__METHOD__, $this->config);
- // set _make_link() accessible
- $_make_link = new \ReflectionMethod($pagination, '_make_link');
- $_make_link->setAccessible(true);
- $test = $_make_link->invoke($pagination, 1);
- $expected = 'welcome/index/?foo=bar&fuel%5B0%5D=php1&fuel%5B1%5D=php2&p=1';
- $this->assertEquals($expected, $test);
- $test = $_make_link->invoke($pagination, 99);
- $expected = 'welcome/index/?foo=bar&fuel%5B0%5D=php1&fuel%5B1%5D=php2&p=99';
- $this->assertEquals($expected, $test);
- }
- public function test_query_string_make_link_by_pagination_url()
- {
- // set Request::$main & $active
- $this->set_request('welcome/index/?foo=bar&fuel[]=php1&fuel[]=php2&p=40');
- $this->set_query_string_config();
- $this->config['pagination_url'] = 'http://docs.fuelphp.com/?foo=bar&fuel[]=php1&fuel[]=php2';
- $pagination = Pagination::forge(__METHOD__, $this->config);
- // set _make_link() accessible
- $_make_link = new \ReflectionMethod($pagination, '_make_link');
- $_make_link->setAccessible(true);
- $test = $_make_link->invoke($pagination, 1);
- $expected = 'http://docs.fuelphp.com/?foo=bar&fuel%5B0%5D=php1&fuel%5B1%5D=php2&p=1';
- $this->assertEquals($expected, $test);
- $test = $_make_link->invoke($pagination, 99);
- $expected = 'http://docs.fuelphp.com/?foo=bar&fuel%5B0%5D=php1&fuel%5B1%5D=php2&p=99';
- $this->assertEquals($expected, $test);
- }
- public function test_query_string_make_link_by_pagination_url_include_page_number()
- {
- // set Request::$main & $active
- $this->set_request('welcome/index/?foo=bar&fuel[]=php1&fuel[]=php2&p=40');
- $this->set_query_string_config();
- $this->config['pagination_url'] = 'http://docs.fuelphp.com/?foo=bar&p=123&fuel[]=php1&fuel[]=php2';
- $pagination = Pagination::forge(__METHOD__, $this->config);
- // set _make_link() accessible
- $_make_link = new \ReflectionMethod($pagination, '_make_link');
- $_make_link->setAccessible(true);
- $test = $_make_link->invoke($pagination, 1);
- $expected = 'http://docs.fuelphp.com/?foo=bar&p=1&fuel%5B0%5D=php1&fuel%5B1%5D=php2';
- $this->assertEquals($expected, $test);
- $test = $_make_link->invoke($pagination, 99);
- $expected = 'http://docs.fuelphp.com/?foo=bar&p=99&fuel%5B0%5D=php1&fuel%5B1%5D=php2';
- $this->assertEquals($expected, $test);
- }
- }
|