pathinfo = $_SERVER['PATH_INFO']; $_SERVER['PATH_INFO'] = '/'.$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); } protected function setUp() { $this->old_base_url = Config::get('base_url'); } public function tearDown() { // remove the fake uri if (property_exists($this, 'pathinfo')) { $_SERVER['PATH_INFO'] = $this->pathinfo; } else { unset($_SERVER['PATH_INFO']); } // 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); // ensure base_url is reset even if an exception occurs Config::set('base_url', $this->old_base_url); } /********************************** * 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, 'wrapper' => "
\n\t{pagination}\n
\n", 'first' => "\n\t{link}\n\n", 'first-marker' => "««", 'first-link' => "\t\t{page}\n", 'first-inactive' => "", 'first-inactive-link' => "", 'previous' => "\n\t{link}\n\n", 'previous-marker' => "«", 'previous-link' => "\t\t{page}\n", 'previous-inactive' => "\n\t{link}\n\n", 'previous-inactive-link' => "\t\t{page}\n", 'regular' => "\n\t{link}\n\n", 'regular-link' => "\t\t{page}\n", 'active' => "\n\t{link}\n\n", 'active-link' => "\t\t{page}\n", 'next' => "\n\t{link}\n\n", 'next-marker' => "»", 'next-link' => "\t\t{page}\n", 'next-inactive' => "\n\t{link}\n\n", 'next-inactive-link' => "\t\t{page}\n", 'last' => "\n\t{link}\n\n", 'last-marker' => "»»", 'last-link' => "\t\t{page}\n", 'last-inactive' => "", 'last-inactive-link' => "", ); } 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); } 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); } public function test_current_page_calculation_from_link() { // set Request::$main & $active $this->set_request('welcome/index/4'); $this->set_uri_segment_config(); $config = array( 'total_items' => 12, 'per_page' => 3, 'uri_segment' => 3, ); $pagination = Pagination::forge(__METHOD__.'-1', $config); $test = $pagination->current_page; $expected = 4; $this->assertEquals($expected, $test); } public function test_current_page_calculation_from_config() { $this->set_request('welcome/index/6'); $config = array( 'per_page' => 3, 'total_items' => 12, 'uri_segment' => 3, 'current_page' => 4, ); $pagination = Pagination::forge(__METHOD__.'-2', $config); $test = $pagination->current_page; $expected = 4; $this->assertEquals($expected, $test); } /** * raw render * */ public function test_pagination_raw_render() { // set Request::$main & $active $this->set_request('welcome/index/5'); $this->set_uri_segment_config(); $pagination = Pagination::forge(__METHOD__, $this->config); $expected = array( 'previous' => array( 'uri' => "http://docs.fuelphp.com/welcome/index/4", 'title' => "«", 'type' => "previous", ), 0 => array( 'uri' => "http://docs.fuelphp.com/welcome/index/3", 'title' => 3, 'type' => "regular", ), 1 => array( 'uri' => "http://docs.fuelphp.com/welcome/index/4", 'title' => 4, 'type' => "regular", ), 2 => array( 'uri' => "#", 'title' => 5, 'type' => "active", ), 3 => array( 'uri' => "http://docs.fuelphp.com/welcome/index/6", 'title' => 6, 'type' => "regular", ), 4 => array( 'uri' => "http://docs.fuelphp.com/welcome/index/7", 'title' => 7, 'type' => "regular", ), 'next' => array( 'uri' => "http://docs.fuelphp.com/welcome/index/6", 'title' => "»", 'type' => "next", ), ); // default link offset of 50%, active is the middle link $test = $pagination->render(true); $this->assertEquals($expected, $test); $expected = array( 'previous' => array( 'uri' => "http://docs.fuelphp.com/welcome/index/4", 'title' => "«", 'type' => "previous", ), 0 => array( 'uri' => "#", 'title' => 5, 'type' => "active", ), 1 => array( 'uri' => "http://docs.fuelphp.com/welcome/index/6", 'title' => 6, 'type' => "regular", ), 2 => array( 'uri' => "http://docs.fuelphp.com/welcome/index/7", 'title' => 7, 'type' => "regular", ), 3 => array( 'uri' => "http://docs.fuelphp.com/welcome/index/8", 'title' => 8, 'type' => "regular", ), 4 => array( 'uri' => "http://docs.fuelphp.com/welcome/index/9", 'title' => 9, 'type' => "regular", ), 'next' => array( 'uri' => "http://docs.fuelphp.com/welcome/index/6", 'title' => "»", 'type' => "next", ), ); $pagination->link_offset = 0; // 0%, active is the first link $test = $pagination->render(true); $this->assertEquals($expected, $test); $expected = array( 'previous' => array( 'uri' => "http://docs.fuelphp.com/welcome/index/4", 'title' => "«", 'type' => "previous", ), 0 => array( 'uri' => "http://docs.fuelphp.com/welcome/index/1", 'title' => 1, 'type' => "regular", ), 1 => array( 'uri' => "http://docs.fuelphp.com/welcome/index/2", 'title' => 2, 'type' => "regular", ), 2 => array( 'uri' => "http://docs.fuelphp.com/welcome/index/3", 'title' => 3, 'type' => "regular", ), 3 => array( 'uri' => "http://docs.fuelphp.com/welcome/index/4", 'title' => 4, 'type' => "regular", ), 4 => array( 'uri' => "#", 'title' => 5, 'type' => "active", ), 'next' => array( 'uri' => "http://docs.fuelphp.com/welcome/index/6", 'title' => "»", 'type' => "next", ), ); $pagination->link_offset = 100; // 100%, active is the last link $test = $pagination->render(true); $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 = ''; $this->assertEquals($expected, $output); $output = $pagination->pages_render(); $output = str_replace(array("\n", "\t"), "", $output); $expected = '12345'; $this->assertEquals($expected, $output); $output = $pagination->next(); $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); } /** * last page * */ public function test_uri_segment_nextlink_inactive() { // set Request::$main & $active $this->set_request('welcome/index/11'); $this->set_uri_segment_config(); $pagination = Pagination::forge(__METHOD__, $this->config); $output = $pagination->next(); $output = str_replace(array("\n", "\t"), "", $output); $expected = ''; $this->assertEquals($expected, $output); $this->set_request('welcome/index/10'); $output = $pagination->pages_render(); $output = str_replace(array("\n", "\t"), "", $output); $expected = '678910'; $this->assertEquals($expected, $output); $output = $pagination->previous(); $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); } /** * 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, 'wrapper' => "
\n\t{pagination}\n
\n", 'first' => "\n\t{link}\n\n", 'first-marker' => "««", 'first-link' => "\t\t{page}\n", 'first-inactive' => "", 'first-inactive-link' => "", 'previous' => "\n\t{link}\n\n", 'previous-marker' => "«", 'previous-link' => "\t\t{page}\n", 'previous-inactive' => "\n\t{link}\n\n", 'previous-inactive-link' => "\t\t{page}\n", 'regular' => "\n\t{link}\n\n", 'regular-link' => "\t\t{page}\n", 'active' => "\n\t{link}\n\n", 'active-link' => "\t\t{page}\n", 'next' => "\n\t{link}\n\n", 'next-marker' => "»", 'next-link' => "\t\t{page}\n", 'next-inactive' => "\n\t{link}\n\n", 'next-inactive-link' => "\t\t{page}\n", 'last' => "\n\t{link}\n\n", 'last-marker' => "»»", 'last-link' => "\t\t{page}\n", 'last-inactive' => "", 'last-inactive-link' => "", ); } 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); } 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 = ''; $this->assertEquals($expected, $output); $output = $pagination->pages_render(); $output = str_replace(array("\n", "\t"), "", $output); $expected = '12345'; $this->assertEquals($expected, $output); $output = $pagination->next(); $output = str_replace(array("\n", "\t"), "", $output); $expected = ''; $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 = ''; $this->assertEquals($expected, $output); $output = $pagination->pages_render(); $output = str_replace(array("\n", "\t"), "", $output); $expected = '678910'; $this->assertEquals($expected, $output); $output = $pagination->previous(); $output = str_replace(array("\n", "\t"), "", $output); $expected = ''; $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); } }