Browse Source

Added tests

Jonathan Boyer 12 years ago
parent
commit
7bdfa54569

+ 1 - 1
src/View/Helper/PaginatorHelper.php

@@ -440,7 +440,7 @@ class PaginatorHelper extends Helper {
 		if (!empty($url['page']) && $url['page'] == 1) {
 			$url['page'] = null;
 		}
-		if (isset($paging['sortDefault']) && isset($paging['directionDefault']) && $url['sort'] == $paging['sortDefault'] && $url['direction'] == $paging['directionDefault'] ) {
+		if (isset($paging['sortDefault']) && isset($paging['directionDefault']) && $url['sort'] === $paging['sortDefault'] && $url['direction'] === $paging['directionDefault'] ) {
 			$url['sort'] = $url['direction'] = null;
 		}
 		return parent::url($url);

+ 30 - 0
tests/TestCase/Controller/Component/PaginatorComponentTest.php

@@ -186,6 +186,36 @@ class PaginatorComponentTest extends TestCase {
 	}
 
 /**
+ * test that default sort and default direction are injectect into request
+ *
+ * @return void
+ */
+	public function testDefaultPaginateParamsIntoRequest() {
+		$settings = array(
+			'order' => ['PaginatorPosts.id' => 'DESC'],
+			'maxLimit' => 10,
+		);
+
+		$table = $this->_getMockPosts(['find']);
+		$query = $this->_getMockFindQuery();
+
+		$table->expects($this->once())
+			->method('find')
+			->with('all', [
+				'conditions' => [],
+				'fields' => null,
+				'limit' => 10,
+				'page' => 1,
+				'order' => ['PaginatorPosts.id' => 'DESC']
+			])
+			->will($this->returnValue($query));
+
+		$this->Paginator->paginate($table, $settings);
+		$this->assertEquals('PaginatorPosts.id', $this->request->params['paging']['PaginatorPosts']['sortDefault']);
+		$this->assertEquals('DESC', $this->request->params['paging']['PaginatorPosts']['directionDefault']);
+	}
+
+/**
  * test that option merging prefers specific models
  *
  * @return void

+ 29 - 0
tests/TestCase/View/Helper/PaginatorHelperTest.php

@@ -735,6 +735,35 @@ class PaginatorHelperTest extends TestCase {
 	}
 
 /**
+ * Test that url generated doesn't include default sort & direction
+ *
+ * @return void
+ */
+	public function testDefaultSortRemovedFromUrl() {
+		Router::setRequestInfo(array(
+			array('plugin' => null, 'controller' => 'articles', 'action' => 'index'),
+			array('base' => '/', 'here' => '/articles/', 'webroot' => '/')
+		));
+		$this->Paginator->request->params['paging'] = array(
+			'Article' => array(
+				'page' => 1, 'current' => 3, 'count' => 13,
+				'prevPage' => false, 'nextPage' => true, 'pageCount' => 8,
+				'sort' => 'Article.title', 'direction' => 'ASC',
+				'sortDefault' => 'Article.title', 'directionDefault' => 'ASC'
+			)
+		);
+		$result = $this->Paginator->next('Next');
+		$expected = array(
+			'li' => array('class' => 'next'),
+			'a'  => array('rel' => 'next', 'href' => '/articles/index?page=2'),
+			'Next',
+			'/a',
+			'/li'
+		);
+		$this->assertTags($result, $expected);
+	}
+
+/**
  * Test the prev() method.
  *
  * @return void