Browse Source

re implement resetUrl (former defaultUrl) and completeUrl methods.

Mark Scherer 10 years ago
parent
commit
ec7f966736

+ 33 - 5
src/View/Helper/HtmlHelper.php

@@ -30,7 +30,8 @@ use Cake\View\View;
 class HtmlHelper extends CoreHtmlHelper {
 
     /**
-     * Creates a default HTML link.
+     * Creates a reset HTML link.
+	 * The prefix and plugin params are resetting to default false.
      *
      * ### Options
      *
@@ -40,20 +41,47 @@ class HtmlHelper extends CoreHtmlHelper {
      * - `confirm` JavaScript confirmation message.
 	 *
      * @param string $title The content to be wrapped by <a> tags.
-     * @param string|array|null $url Cake-relative URL or array of URL parameters, or
+     * @param string|array|null $url URL or array of URL parameters, or
      *   external URL (starts with http://)
      * @param array $options Array of options and HTML attributes.
      * @return string An `<a />` element.
-     * @link http://book.cakephp.org/3.0/en/views/helpers/html.html#creating-links
      */
-    public function defaultLink($title, $url = null, array $options = []) {
-
+    public function resetLink($title, $url = null, array $options = []) {
 		if (is_array($url)) {
 			$url += ['prefix' => false, 'plugin' => false];
 		}
 		return parent::link($title, $url, $options);
     }
 
+	/**
+	 * Keep query string params for pagination/filter for this link,
+	 * e.g. after edit action.
+	 *
+	 * ### Options
+	 *
+	 * - `escape` Set to false to disable escaping of title and attributes.
+	 * - `escapeTitle` Set to false to disable escaping of title. Takes precedence
+	 *   over value of `escape`)
+	 * - `confirm` JavaScript confirmation message.
+	 *
+	 * @param string $title The content to be wrapped by <a> tags.
+	 * @param string|array|null $url URL or array of URL parameters, or
+	 *   external URL (starts with http://)
+	 * @param array $options Array of options and HTML attributes.
+	 * @return string An `<a />` element.
+	 * @return string Link
+	 */
+	public function completeLink($title, $url = null, array $options = []) {
+		if (is_array($url)) {
+			// Add query strings
+			if (!isset($url['?'])) {
+				$url['?'] = [];
+			}
+			$url['?'] += $this->request->query;
+		}
+		return parent::link($title, $url, $options);
+	}
+
     /**
      * Event listeners.
      *

+ 7 - 4
src/View/Helper/TreeHelper.php

@@ -117,12 +117,15 @@ class TreeHelper extends Helper {
 	 *	'splitCount' => the number of "parallel" types. defaults to null (disabled) set the splitCount,
 	 *		and optionally set the splitDepth to get parallel lists
 	 *
-	 * @param array $data data to loop on
-	 * @param array $config
-	 * @return string html representation of the passed data
+	 * @param array|Query $data Data to loop over
+	 * @param array $config Config
+	 * @return string HTML representation of the passed data
 	 * @throws \Exception
 	 */
-	public function generate(array $data, array $config = []) {
+	public function generate($data, array $config = []) {
+		if (is_object($data)) {
+			$data = $data->toArray();
+		}
 		if (!$data) {
 			return '';
 		}

+ 29 - 5
src/View/Helper/UrlHelper.php

@@ -26,21 +26,45 @@ use Cake\View\View;
 class UrlHelper extends CoreUrlHelper {
 
 	/**
-	 * Returns a URL based on provided parameters.
+	 * @deprecated
+	 */
+	public function defaultBuild($url = null, $full = false) {
+		return $this->reset($url, $full);
+	}
+
+	/**
+	 * Creates a reset URL.
+	 * The prefix and plugin params are resetting to default false.
 	 *
-	 * @param string|array $url Either a relative string url like `/products/view/23` or
-	 *    an array of URL parameters. Using an array for URLs will allow you to leverage
-	 *    the reverse routing features of CakePHP.
+	 * @param string|array $url URL.
 	 * @param bool $full If true, the full base URL will be prepended to the result
 	 * @return string Full translated URL with base path.
 	 */
-	public function defaultBuild($url = null, $full = false) {
+	public function reset($url = null, $full = false) {
 		if (is_array($url)) {
 			$url += ['prefix' => false, 'plugin' => false];
 		}
 		return parent::build($url, $full);
 	}
 
+	/**
+	 * Returns a URL based on provided parameters.
+	 *
+	 * @param string|array $url URL.
+	 * @param bool $full If true, the full base URL will be prepended to the result
+	 * @return string Full translated URL with base path.
+	 */
+	public function complete($url = null, $full = false) {
+		if (is_array($url)) {
+			// Add query strings
+			if (!isset($url['?'])) {
+				$url['?'] = [];
+			}
+			$url['?'] += $this->request->query;
+		}
+		return parent::build($url, $full);
+	}
+
     /**
      * Event listeners.
      *

+ 90 - 0
tests/TestCase/View/Helper/HtmlHelperTest.php

@@ -0,0 +1,90 @@
+<?php
+namespace Tools\TestCase\View\Helper;
+
+use Tools\View\Helper\HtmlHelper;
+use Tools\TestSuite\TestCase;
+use Cake\View\View;
+use Cake\Core\Configure;
+use Tools\Utility\Time;
+use Cake\Network\Request;
+use Cake\Routing\Router;
+
+/**
+ * Datetime Test Case
+ *
+ */
+class TimeHelperTest extends TestCase {
+
+	public function setUp() {
+		parent::setUp();
+
+		$this->Html = new HtmlHelper(new View(null));
+		$this->Html->request = new Request();
+		$this->Html->request->webroot = '';
+		$this->Html->Url->request = $this->Html->request;
+	}
+
+	/**
+	 * Tests
+	 *
+	 * @return void
+	 */
+	public function testResetLink() {
+		Router::connect('/:controller/:action/*');
+
+		$result = $this->Html->resetLink('Foo', ['controller' => 'foobar', 'action' => 'test']);
+		$expected = '<a href="/foobar/test">Foo</a>';
+		$this->assertEquals($expected, $result);
+
+		$this->Html->request->here = '/admin/foobar/test';
+		$this->Html->request->params['admin'] = true;
+		$this->Html->request->params['prefix'] = 'admin';
+		Router::reload();
+        Router::connect('/:controller/:action/*');
+		Router::prefix('admin', function ($routes) {
+			$routes->connect('/:controller/:action/*');
+		});
+
+		$result = $this->Html->link('Foo', ['prefix' => 'admin', 'controller' => 'foobar', 'action' => 'test']);
+		$expected = '<a href="/admin/foobar/test">Foo</a>';
+		$this->assertEquals($expected, $result);
+
+		$result = $this->Html->link('Foo', ['controller' => 'foobar', 'action' => 'test']);
+		$expected = '<a href="/admin/foobar/test">Foo</a>';
+		debug($result);
+		//$this->assertEquals($expected, $result);
+
+		$result = $this->Html->resetLink('Foo', ['controller' => 'foobar', 'action' => 'test']);
+		$expected = '<a href="/foobar/test">Foo</a>';
+		$this->assertEquals($expected, $result);
+	}
+
+	/**
+	 * Tests
+	 *
+	 * @return void
+	 */
+	public function testCompleteLink() {
+		$this->Html->request->query['x'] = 'y';
+
+		$result = $this->Html->completeLink('Foo', ['action' => 'test']);
+		$expected = '<a href="/test?x=y">Foo</a>';
+		$this->assertEquals($expected, $result);
+
+		$result = $this->Html->completeLink('Foo', ['action' => 'test', '?' => ['a' => 'b']]);
+		$expected = '<a href="/test?a=b&amp;x=y">Foo</a>';
+		$this->assertEquals($expected, $result);
+	}
+
+	/**
+	 * TearDown method
+	 *
+	 * @return void
+	 */
+	public function tearDown() {
+		parent::tearDown();
+
+		unset($this->Html);
+	}
+
+}

+ 89 - 0
tests/TestCase/View/Helper/UrlHelperTest.php

@@ -0,0 +1,89 @@
+<?php
+namespace Tools\TestCase\View\Helper;
+
+use Tools\View\Helper\UrlHelper;
+use Tools\TestSuite\TestCase;
+use Cake\View\View;
+use Cake\Core\Configure;
+use Tools\Utility\Time;
+use Cake\Network\Request;
+use Cake\Routing\Router;
+
+/**
+ * Datetime Test Case
+ *
+ */
+class TimeHelperTest extends TestCase {
+
+	public function setUp() {
+		parent::setUp();
+
+		$this->Url = new UrlHelper(new View(null));
+		$this->Url->request = new Request();
+		$this->Url->request->webroot = '';
+	}
+
+	/**
+	 * Tests
+	 *
+	 * @return void
+	 */
+	public function testReset() {
+		Router::connect('/:controller/:action/*');
+
+		$result = $this->Url->reset(['controller' => 'foobar', 'action' => 'test']);
+		$expected = '/foobar/test';
+		$this->assertEquals($expected, $result);
+
+		$this->Url->request->here = '/admin/foobar/test';
+		$this->Url->request->params['admin'] = true;
+		$this->Url->request->params['prefix'] = 'admin';
+		Router::reload();
+        Router::connect('/:controller/:action/*');
+		Router::prefix('admin', function ($routes) {
+			$routes->connect('/:controller/:action/*');
+		});
+
+		$result = $this->Url->build(['prefix' => 'admin', 'controller' => 'foobar', 'action' => 'test']);
+		$expected = '/admin/foobar/test';
+		$this->assertEquals($expected, $result);
+
+		$result = $this->Url->build(['controller' => 'foobar', 'action' => 'test']);
+		$expected = '/admin/foobar/test';
+		debug($result);
+		//$this->assertEquals($expected, $result);
+
+		$result = $this->Url->reset(['controller' => 'foobar', 'action' => 'test']);
+		$expected = '/foobar/test';
+		$this->assertEquals($expected, $result);
+	}
+
+	/**
+	 * Tests
+	 *
+	 * @return void
+	 */
+	public function testComplete() {
+		$this->Url->request->query['x'] = 'y';
+
+		$result = $this->Url->complete(['action' => 'test']);
+		$expected = '/test?x=y';
+		$this->assertEquals($expected, $result);
+
+		$result = $this->Url->complete(['action' => 'test', '?' => ['a' => 'b']]);
+		$expected = '/test?a=b&amp;x=y';
+		$this->assertEquals($expected, $result);
+	}
+
+	/**
+	 * TearDown method
+	 *
+	 * @return void
+	 */
+	public function tearDown() {
+		parent::tearDown();
+
+		unset($this->Url);
+	}
+
+}