From 18adbb643940ebef027b73df06c1020e72a77cfc Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Mon, 29 Sep 2014 04:02:23 +0000 Subject: [PATCH] In `paginate_links()`, ensure that query string args are propagated to the resulting paginated links. Adds unit tests that use `DOMDocument` since `assertTag` is being deprecated - see #29545, [29746]. Props obenland, wonderboymusic. Fixes #29636. git-svn-id: https://develop.svn.wordpress.org/trunk@29780 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/general-template.php | 2 +- tests/phpunit/tests/general/paginateLinks.php | 43 ++++++++++++++++++- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/general-template.php b/src/wp-includes/general-template.php index 61665ad5af..5acf4ad605 100644 --- a/src/wp-includes/general-template.php +++ b/src/wp-includes/general-template.php @@ -2485,7 +2485,7 @@ function paginate_links( $args = '' ) { 'end_size' => 1, 'mid_size' => 2, 'type' => 'plain', - 'add_args' => false, // array of query args to add + 'add_args' => $query_args, // array of query args to add 'add_fragment' => '', 'before_page_number' => '', 'after_page_number' => '' diff --git a/tests/phpunit/tests/general/paginateLinks.php b/tests/phpunit/tests/general/paginateLinks.php index 972864db6e..8fae1f61f1 100644 --- a/tests/phpunit/tests/general/paginateLinks.php +++ b/tests/phpunit/tests/general/paginateLinks.php @@ -126,11 +126,11 @@ EXPECTED; $expected_attributes = array( array( - 'href' => 'http://' . WP_TESTS_DOMAIN . '/', + 'href' => home_url( '/' ), 'class' => 'prev page-numbers' ), array( - 'href' => 'http://' . WP_TESTS_DOMAIN . '/', + 'href' => home_url( '/' ), 'class' => 'page-numbers' ) ); @@ -177,4 +177,43 @@ EXPECTED; $this->assertEquals( get_pagenum_link( 2 ), $href ); } + function add_query_arg( $url ) { + return add_query_arg( array( 'foo' => 'bar' ), $url ); + } + + /** + * @ticket 29636 + */ + function test_paginate_links_query_args() { + add_filter( 'get_pagenum_link', array( $this, 'add_query_arg' ) ); + $links = paginate_links( array( + 'current' => 2, + 'total' => 5, + 'end_size' => 1, + 'mid_size' => 1, + 'type' => 'array', + ) ); + remove_filter( 'get_pagenum_link', array( $this, 'add_query_arg' ) ); + + $document = new DOMDocument(); + $document->preserveWhiteSpace = false; + + // All links should have foo=bar arguments: + $data = array( + 0 => home_url( '/?foo=bar' ), + 1 => home_url( '/?foo=bar' ), + 3 => home_url( '/?paged=3&foo=bar' ), + 5 => home_url( '/?paged=5&foo=bar' ), + 6 => home_url( '/?paged=3&foo=bar' ), + ); + + foreach ( $data as $index => $expected_href ) { + $document->loadHTML( $links[ $index ] ); + $tag = $document->getElementsByTagName( 'a' )->item( 0 ); + $this->assertNotNull( $tag ); + + $href = $tag->attributes->getNamedItem( 'href' )->value; + $this->assertEquals( $expected_href, $href ); + } + } }