Browse Source

Merge branch 'cake3' of git://github.com/dereuromark/cakephp-tools into cake3

Mark Scherer 10 years ago
parent
commit
3e36393ef9

+ 0 - 1
.travis.yml

@@ -24,7 +24,6 @@ matrix:
       env: COVERALLS=1 DEFAULT=0
 
   allow_failures:
-    - env: DB=sqlite db_class='Cake\Database\Driver\Sqlite' db_dsn='sqlite::memory:'
     - env: DB=pgsql db_class='Cake\Database\Driver\Postgres' db_dsn='pgsql:host=127.0.0.1;dbname=cakephp_test' db_database="cakephp_test" db_username='postgres' db_password=''
     - php: 5.4
       env: PHPCS=1 DEFAULT=0

+ 1 - 1
composer.json

@@ -15,7 +15,7 @@
 	"require":{
 		"php": ">=5.4",
 		"cakephp/cakephp": "~3.0",
-		"dereuromark/cakephp-shim": "3.0.*-dev"
+		"dereuromark/cakephp-shim": "dev-master"
 	},
 	"autoload": {
 		"psr-4": {

+ 9 - 0
docs/Controller/Controller.md

@@ -0,0 +1,9 @@
+# Controller
+
+When using the Tools plugin Controller class you can set your pagination defaults via
+Configure
+```
+'Paginator' => [
+    'limit' => ... // etc
+]
+```

+ 2 - 36
src/Controller/Controller.php

@@ -1,26 +1,13 @@
 <?php
 namespace Tools\Controller;
 
-use Cake\Controller\Controller as CakeController;
+use Shim\Controller\Controller as ShimController;
 use Cake\Core\Configure;
-use Cake\Event\Event;
 
 /**
  * DRY Controller stuff
  */
-class Controller extends CakeController {
-
-	/**
-	 * Add headers for IE8 etc to fix caching issues in those stupid browsers.
-	 *
-	 * @return void
-	 */
-	public function disableCache() {
-		$this->response->header([
-			'Pragma' => 'no-cache',
-		]);
-		$this->response->disableCache();
-	}
+class Controller extends ShimController {
 
 	/**
 	 * Handles automatic pagination of model records.
@@ -37,25 +24,4 @@ class Controller extends CakeController {
 		return parent::paginate($object);
 	}
 
-	/**
-	 * Hook to monitor headers being sent.
-	 *
-	 * This, if desired, adds a check if your controller actions are cleanly built and no headers
-	 * or output is being sent prior to the response class, which should be the only one doing this.
-	 *
-	 * @param Event $event An Event instance
-	 * @return void
-	 */
-	public function afterFilter(Event $event) {
-		if (Configure::read('App.monitorHeaders') && $this->name !== 'Error' && php_sapi_name() !== 'cli') {
-			if (headers_sent($filename, $linenum)) {
-				$message = sprintf('Headers already sent in %s on line %s', $filename, $linenum);
-				if (Configure::read('debug')) {
-					throw new \Exception($message);
-				}
-				trigger_error($message);
-			}
-		}
-	}
-
 }

+ 1 - 1
src/Model/Behavior/PasswordableBehavior.php

@@ -13,7 +13,7 @@ if (!defined('PWD_MIN_LENGTH')) {
 	define('PWD_MIN_LENGTH', 6);
 }
 if (!defined('PWD_MAX_LENGTH')) {
-	define('PWD_MAX_LENGTH', 20);
+	define('PWD_MAX_LENGTH', 50);
 }
 
 /**

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

@@ -205,7 +205,7 @@ class FormatHelper extends TextHelper {
 		];
 		$options += $defaults;
 		$icon = (array)$icon;
-		$class = [];
+		$class = [$options['namespace']];
 		foreach ($icon as $i) {
 			$class[] = $options['namespace'] . '-' . $i;
 		}

+ 94 - 0
src/View/Helper/HtmlHelper.php

@@ -0,0 +1,94 @@
+<?php
+/**
+ * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * @link          http://cakephp.org CakePHP(tm) Project
+ * @since         0.9.1
+ * @license       http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Tools\View\Helper;
+
+use Cake\Core\Configure;
+use Cake\Network\Response;
+use Cake\View\Helper\HtmlHelper as CoreHtmlHelper;
+use Cake\View\StringTemplateTrait;
+use Cake\View\View;
+
+/**
+ * Html Helper class for easy use of HTML widgets.
+ *
+ * HtmlHelper encloses all methods needed while working with HTML pages.
+ *
+ * @link http://book.cakephp.org/3.0/en/views/helpers/html.html
+ */
+class HtmlHelper extends CoreHtmlHelper {
+
+    /**
+     * Creates a reset HTML link.
+	 * The prefix and plugin params are resetting to default false.
+     *
+     * ### 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.
+     */
+    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.
+     *
+     * @return array
+     */
+    public function implementedEvents()
+    {
+        return [];
+    }
+}

+ 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 '';
 		}

+ 77 - 0
src/View/Helper/UrlHelper.php

@@ -0,0 +1,77 @@
+<?php
+/**
+ * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * @link          http://cakephp.org CakePHP(tm) Project
+ * @since         0.9.1
+ * @license       http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Tools\View\Helper;
+
+use Cake\Core\Configure;
+use Cake\Network\Response;
+use Cake\View\Helper\UrlHelper as CoreUrlHelper;
+use Cake\View\StringTemplateTrait;
+use Cake\View\View;
+
+/**
+ * Url Helper class.
+ */
+class UrlHelper extends CoreUrlHelper {
+
+	/**
+	 * @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 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 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.
+     *
+     * @return array
+     */
+    public function implementedEvents()
+    {
+        return [];
+    }
+}

+ 13 - 7
tests/TestCase/Controller/ControllerTest.php

@@ -9,11 +9,14 @@ use Cake\Network\Request;
 use Cake\Network\Session;
 use Tools\TestSuite\TestCase;
 use Tools\Controller\Controller;
+use Cake\ORM\TableRegistry;
 
 /**
  */
 class ControllerTest extends TestCase {
 
+	public $fixtures = ['plugin.Tools.ToolsUsers'];
+
 	public $Controller;
 
 	public function setUp() {
@@ -32,16 +35,19 @@ class ControllerTest extends TestCase {
 	}
 
 	/**
-	 * CommonComponentTest::testLoadComponent()
-	 *
 	 * @return void
 	 */
-	public function testDisableCache() {
-		$this->Controller->disableCache();
+	public function testPaginate() {
+		Configure::write('Paginator.limit', 2);
+
+		$ToolsUser = TableRegistry::get('ToolsUsers');
+
+		$count = $ToolsUser->find('count');
+		$this->assertTrue($count > 3);
 
-		$result = $this->Controller->response->header();
-		$expected = ['Pragma', 'Expires', 'Last-Modified', 'Cache-Control'];
-		$this->assertSame($expected, array_keys($result));
+		$this->Controller->loadModel('ToolsUsers');
+		$result = $this->Controller->paginate('ToolsUsers');
+		$this->assertSame(2, count($result->toArray()));
 	}
 
 }

+ 4 - 4
tests/TestCase/View/Helper/FormatHelperTest.php

@@ -136,19 +136,19 @@ class FormatHelperTest extends TestCase {
 	 */
 	public function testFontIcon() {
 		$result = $this->Format->fontIcon('signin');
-		$expected = '<i class="fa-signin"></i>';
+		$expected = '<i class="fa fa-signin"></i>';
 		$this->assertEquals($expected, $result);
 
 		$result = $this->Format->fontIcon('signin', ['rotate' => 90]);
-		$expected = '<i class="fa-signin fa-rotate-90"></i>';
+		$expected = '<i class="fa fa-signin fa-rotate-90"></i>';
 		$this->assertEquals($expected, $result);
 
 		$result = $this->Format->fontIcon('signin', ['size' => 5, 'extra' => ['muted']]);
-		$expected = '<i class="fa-signin fa-muted fa-5x"></i>';
+		$expected = '<i class="fa fa-signin fa-muted fa-5x"></i>';
 		$this->assertEquals($expected, $result);
 
 		$result = $this->Format->fontIcon('signin', ['size' => 5, 'extra' => ['muted'], 'namespace' => 'icon']);
-		$expected = '<i class="icon-signin icon-muted icon-5x"></i>';
+		$expected = '<i class="icon icon-signin icon-muted icon-5x"></i>';
 		$this->assertEquals($expected, $result);
 	}
 

+ 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 HtmlHelperTest 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 UrlHelperTest 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);
+	}
+
+}

+ 0 - 14
tests/bootstrap.php

@@ -77,20 +77,6 @@ if (!getenv('db_class')) {
 	putenv('db_dsn=sqlite::memory:');
 }
 
-if (WINDOWS) {
-	Cake\Datasource\ConnectionManager::config('test', [
-		'className' => 'Cake\Database\Connection',
-		'driver' => 'Cake\Database\Driver\Mysql',
-		'database' => 'cake_test',
-		'username' => 'root',
-		'password' => '',
-		'timezone' => 'UTC',
-		'quoteIdentifiers' => true,
-		'cacheMetadata' => true,
-	]);
-	return;
-}
-
 Cake\Datasource\ConnectionManager::config('test', [
 	'className' => 'Cake\Database\Connection',
 	'driver' => getenv('db_class'),