Browse Source

Merge branch 'master' into 3.1

Jose Lorenzo Rodriguez 10 years ago
parent
commit
c5990f9a18

+ 0 - 5
src/Cache/Engine/RedisEngine.php

@@ -92,11 +92,6 @@ class RedisEngine extends CacheEngine
     protected function _connect()
     {
         try {
-            $server = $this->_config['host'];
-            if (empty($server) && !empty($this->_config['server'])) {
-                $server = $this->_config['server'];
-            }
-
             $this->_Redis = new \Redis();
             if (!empty($this->settings['unix_socket'])) {
                 $return = $this->_Redis->connect($this->settings['unix_socket']);

+ 6 - 2
src/Collection/Iterator/SortIterator.php

@@ -46,7 +46,7 @@ class SortIterator extends Collection
      *
      * The callback will receive as first argument each of the elements in $items,
      * the value returned in the callback will be used as the value for sorting such
-     * element. Please not that the callback function could be called more than once
+     * element. Please note that the callback function could be called more than once
      * per element.
      *
      * @param array|\Traversable $items The values to sort
@@ -67,7 +67,11 @@ class SortIterator extends Collection
         $callback = $this->_propertyExtractor($callback);
         $results = [];
         foreach ($items as $key => $value) {
-            $results[$key] = $callback($value);
+            $value = $callback($value);
+            if ($value instanceof \DateTime && $type === SORT_NUMERIC) {
+                $value = $value->format('U');
+            }
+            $results[$key] = $value;
         }
 
         $dir === SORT_DESC ? arsort($results, $type) : asort($results, $type);

+ 0 - 1
src/Controller/Component/RequestHandlerComponent.php

@@ -539,7 +539,6 @@ class RequestHandlerComponent extends Component
     public function renderAs(Controller $controller, $type, array $options = [])
     {
         $defaults = ['charset' => 'UTF-8'];
-        $view = null;
         $viewClassMap = $this->viewClassMap();
 
         if (Configure::read('App.encoding') !== null) {

+ 1 - 1
src/Database/Expression/TupleComparison.php

@@ -92,7 +92,7 @@ class TupleComparison extends Comparison
 
             $type = $this->_type;
             $multiType = is_array($type);
-            $isMulti = $this->isMulti($i, $type);
+            $isMulti = $this->isMulti();
             $type = $multiType ? $type : str_replace('[]', '', $type);
             $type = $type ?: null;
 

+ 2 - 2
src/Database/Query.php

@@ -1384,8 +1384,8 @@ class Query implements ExpressionInterface, IteratorAggregate
      *
      * ```
      *
-     * $expression = $query->newExpression(); // Returns an empty expression object
-     * $expression = $query->newExpression('Table.column = Table2.column'); // Return a raw SQL expression
+     * $expression = $query->newExpr(); // Returns an empty expression object
+     * $expression = $query->newExpr('Table.column = Table2.column'); // Return a raw SQL expression
      * ```
      *
      * @param mixed $rawExpression A string, array or anything you want wrapped in an expression object

+ 1 - 1
src/Shell/Task/UnloadTask.php

@@ -34,7 +34,7 @@ class UnloadTask extends Shell
      * Execution method always used for tasks.
      *
      * @param string $plugin The plugin name.
-     * @return boolean if action passed.
+     * @return bool if action passed.
      */
     public function main($plugin = null)
     {

+ 14 - 10
src/Utility/Hash.php

@@ -300,12 +300,10 @@ class Hash
 
         foreach ($data as $k => $v) {
             if (static::_matchToken($k, $token)) {
-                if ($conditions && static::_matches($v, $conditions)) {
-                    $data[$k] = array_merge($v, $values);
-                    continue;
-                }
-                if (!$conditions) {
-                    $data[$k] = static::insert($v, $nextPath, $values);
+                if (!$conditions || static::_matches($v, $conditions)) {
+                    $data[$k] = $nextPath
+                        ? static::insert($v, $nextPath, $values)
+                        : array_merge($v, (array)$values);
                 }
             }
         }
@@ -392,11 +390,17 @@ class Hash
         foreach ($data as $k => $v) {
             $match = static::_matchToken($k, $token);
             if ($match && is_array($v)) {
-                if ($conditions && static::_matches($v, $conditions)) {
-                    unset($data[$k]);
-                    continue;
+                if ($conditions) {
+                    if (static::_matches($v, $conditions)) {
+                        if ($nextPath) {
+                            $data[$k] = static::remove($v, $nextPath);
+                        } else {
+                            unset($data[$k]);
+                        }
+                    }
+                } else {
+                    $data[$k] = static::remove($v, $nextPath);
                 }
-                $data[$k] = static::remove($v, $nextPath);
                 if (empty($data[$k])) {
                     unset($data[$k]);
                 }

+ 4 - 1
src/View/Helper/FormHelper.php

@@ -2210,7 +2210,10 @@ class FormHelper extends Helper
     protected function _datetimeOptions($options)
     {
         foreach ($this->_datetimeParts as $type) {
-            if (!isset($options[$type])) {
+            if (!array_key_exists($type, $options)) {
+                $options[$type] = [];
+            }
+            if ($options[$type] === true) {
                 $options[$type] = [];
             }
 

+ 3 - 2
src/View/Helper/PaginatorHelper.php

@@ -603,7 +603,8 @@ class PaginatorHelper extends Helper
      * - `before` Content to be inserted before the numbers, but after the first links.
      * - `after` Content to be inserted after the numbers, but before the last links.
      * - `model` Model to create numbers for, defaults to PaginatorHelper::defaultModel()
-     * - `modulus` how many numbers to include on either side of the current page, defaults to 8.
+     * - `modulus` How many numbers to include on either side of the current page, defaults to 8.
+     *    Set to `false` to disable and to show all numbers.
      * - `first` Whether you want first links generated, set to an integer to define the number of 'first'
      *    links to generate.
      * - `last` Whether you want last links generated, set to an integer to define the number of 'last'
@@ -641,7 +642,7 @@ class PaginatorHelper extends Helper
             $templater->{$method}($options['templates']);
         }
 
-        if ($options['modulus'] && $params['pageCount'] > $options['modulus']) {
+        if ($options['modulus'] !== false && $params['pageCount'] > $options['modulus']) {
             $out = $this->_modulusNumbers($templater, $params, $options);
         } else {
             $out = $this->_numbers($templater, $params, $options);

+ 24 - 4
src/View/Widget/SelectBoxWidget.php

@@ -39,8 +39,9 @@ class SelectBoxWidget extends BasicWidget
      *    When true, the select element will be disabled.
      * - `val` - Either a string or an array of options to mark as selected.
      * - `empty` - Set to true to add an empty option at the top of the
-     *   option elements. Set to a string to define the display value of the
-     *   empty option.
+     *   option elements. Set to a string to define the display text of the
+     *   empty option. If an array is used the key will set the value of the empty
+     *   option while, the value will set the display text.
      * - `escape` - Set to false to disable HTML escaping.
      *
      * ### Options format
@@ -149,8 +150,7 @@ class SelectBoxWidget extends BasicWidget
         }
 
         if (!empty($data['empty'])) {
-            $value = $data['empty'] === true ? '' : $data['empty'];
-            $options = ['' => $value] + (array)$options;
+            $options = $this->_emptyValue($data['empty']) + (array)$options;
         }
         if (empty($options)) {
             return [];
@@ -166,6 +166,26 @@ class SelectBoxWidget extends BasicWidget
     }
 
     /**
+     * Generate the empty value based on the input.
+     *
+     * @param string|bool|array $value The provided empty value.
+     * @return array The generated option key/value.
+     */
+    protected function _emptyValue($value)
+    {
+        if ($value === true) {
+            return ['' => ''];
+        }
+        if (is_scalar($value)) {
+            return ['' => $value];
+        }
+        if (is_array($value)) {
+            return $value;
+        }
+        return [];
+    }
+
+    /**
      * Render the contents of an optgroup element.
      *
      * @param string $label The optgroup label text

+ 42 - 0
tests/TestCase/Collection/Iterator/SortIteratorTest.php

@@ -16,6 +16,7 @@ namespace Cake\Test\TestCase\Collection\Iterator;
 
 use ArrayObject;
 use Cake\Collection\Iterator\SortIterator;
+use Cake\I18n\Time;
 use Cake\TestSuite\TestCase;
 
 /**
@@ -191,4 +192,45 @@ class SortIteratorTest extends TestCase
         ];
         $this->assertEquals($expected, $sorted->toList());
     }
+
+    /**
+     * Tests sorting datetime
+     *
+     * @return void
+     */
+    public function testSortDateTime()
+    {
+        $items = new ArrayObject([
+            new \DateTime('2014-07-21'),
+            new \DateTime('2015-06-30'),
+            new \DateTime('2013-08-12')
+        ]);
+        $a = new \DateTime();
+
+        $callback = function ($a) {
+            return $a->add(new \DateInterval('P1Y'));
+        };
+        $sorted = new SortIterator($items, $callback);
+        $expected = [
+            new \DateTime('2016-06-30'),
+            new \DateTime('2015-07-21'),
+            new \DateTime('2014-08-12')
+
+        ];
+        $this->assertEquals($expected, $sorted->toList());
+
+        $items = new ArrayObject([
+            new \DateTime('2014-07-21'),
+            new \DateTime('2015-06-30'),
+            new \DateTime('2013-08-12')
+        ]);
+
+        $sorted = new SortIterator($items, $callback, SORT_ASC);
+        $expected = [
+            new \DateTime('2014-08-12'),
+            new \DateTime('2015-07-21'),
+            new \DateTime('2016-06-30'),
+        ];
+        $this->assertEquals($expected, $sorted->toList());
+    }
 }

+ 22 - 0
tests/TestCase/Utility/HashTest.php

@@ -1476,6 +1476,17 @@ class HashTest extends TestCase
             4 => ['Item' => ['id' => 5, 'title' => 'fifth']],
         ];
         $this->assertEquals($expected, $result);
+
+        $data[3]['testable'] = true;
+        $result = Hash::insert($data, '{n}[testable].Item[id=/\b2|\b4/].test', 2);
+        $expected = [
+            0 => ['Item' => ['id' => 1, 'title' => 'first']],
+            1 => ['Item' => ['id' => 2, 'title' => 'second']],
+            2 => ['Item' => ['id' => 3, 'title' => 'third']],
+            3 => ['Item' => ['id' => 4, 'title' => 'fourth', 'test' => 2], 'testable' => true],
+            4 => ['Item' => ['id' => 5, 'title' => 'fifth']],
+        ];
+        $this->assertEquals($expected, $result);
     }
 
     /**
@@ -1606,6 +1617,17 @@ class HashTest extends TestCase
             4 => ['Item' => ['id' => 5, 'title' => 'fifth']],
         ];
         $this->assertEquals($expected, $result);
+
+        $data[3]['testable'] = true;
+        $result = Hash::remove($data, '{n}[testable].Item[id=/\b2|\b4/].title');
+        $expected = [
+            0 => ['Item' => ['id' => 1, 'title' => 'first']],
+            1 => ['Item' => ['id' => 2, 'title' => 'second']],
+            2 => ['Item' => ['id' => 3, 'title' => 'third']],
+            3 => ['Item' => ['id' => 4], 'testable' => true],
+            4 => ['Item' => ['id' => 5, 'title' => 'fifth']],
+        ];
+        $this->assertEquals($expected, $result);
     }
 
     /**

+ 20 - 0
tests/TestCase/View/Helper/FormHelperTest.php

@@ -5438,6 +5438,26 @@ class FormHelperTest extends TestCase
     }
 
     /**
+     * test datetime second=true
+     *
+     * @return void
+     */
+    public function testDateTimeSecondOptions()
+    {
+        $result = $this->Form->dateTime('updated', ['second' => true]);
+        $this->assertContains('updated[second]', $result, 'Should have seconds');
+
+        $result = $this->Form->dateTime('updated', ['second' => []]);
+        $this->assertContains('updated[second]', $result, 'Should have seconds');
+
+        $result = $this->Form->dateTime('updated', ['second' => null]);
+        $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
+
+        $result = $this->Form->dateTime('updated', ['second' => false]);
+        $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
+    }
+
+    /**
      * testMonth method
      *
      * @return void

+ 44 - 2
tests/TestCase/View/Helper/PaginatorHelperTest.php

@@ -1379,8 +1379,8 @@ class PaginatorHelperTest extends TestCase
                 'pageCount' => 3,
             ]
         ];
-        $options = ['modulus' => 10];
-        $result = $this->Paginator->numbers($options);
+
+        $result = $this->Paginator->numbers(['modulus' => 10]);
         $expected = [
             ['li' => ['class' => 'active']], '<a href=""', '1', '/a', '/li',
             ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
@@ -1535,6 +1535,48 @@ class PaginatorHelperTest extends TestCase
             ['li' => []], ['a' => ['href' => '/index?page=4897']], '4897', '/a', '/li',
         ];
         $this->assertHtml($expected, $result);
+
+        $this->Paginator->request->params['paging']['Client']['page'] = 3;
+        $result = $this->Paginator->numbers(['first' => 2, 'modulus' => 0, 'last' => 2]);
+        $expected = [
+            ['li' => []], ['a' => ['href' => '/index']], '1', '/a', '/li',
+            ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
+            ['li' => ['class' => 'active']], '<a href=""', '3', '/a', '/li',
+            ['li' => ['class' => 'ellipsis']], '...', '/li',
+            ['li' => []], ['a' => ['href' => '/index?page=4896']], '4896', '/a', '/li',
+            ['li' => []], ['a' => ['href' => '/index?page=4897']], '4897', '/a', '/li',
+        ];
+        $this->assertHtml($expected, $result);
+    }
+
+    /**
+     * Tests that disabling modulus displays all page links.
+     *
+     * @return void
+     */
+    public function testModulusDisabled()
+    {
+        $this->Paginator->request->params['paging'] = [
+            'Client' => [
+                'page' => 4,
+                'current' => 2,
+                'count' => 30,
+                'prevPage' => 1,
+                'nextPage' => 1,
+                'pageCount' => 6,
+            ]
+        ];
+
+        $result = $this->Paginator->numbers(['modulus' => false]);
+        $expected = [
+            ['li' => []], '<a href="/index"', '1', '/a', '/li',
+            ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
+            ['li' => []], ['a' => ['href' => '/index?page=3']], '3', '/a', '/li',
+            ['li' => ['class' => 'active']], ['a' => ['href' => '']], '4', '/a', '/li',
+            ['li' => []], ['a' => ['href' => '/index?page=5']], '5', '/a', '/li',
+            ['li' => []], ['a' => ['href' => '/index?page=6']], '6', '/a', '/li',
+        ];
+        $this->assertHtml($expected, $result);
     }
 
     /**

+ 17 - 0
tests/TestCase/View/Widget/DateTimeWidgetTest.php

@@ -110,6 +110,23 @@ class DateTimeWidgetTest extends TestCase
     }
 
     /**
+     * Test empty with custom values.
+     *
+     * @return void
+     */
+    public function testRenderEmptyCustom()
+    {
+        $result = $this->DateTime->render([
+            'val' => '',
+            'year' => [
+                'empty' => ['nope' => '(choose one)'],
+            ]
+        ], $this->context);
+        $this->assertContains('<option value="nope">(choose one)</option>', $result);
+        $this->assertNotContains('<optgroup', $result, 'No optgroups should be present.');
+    }
+
+    /**
      * Data provider for testing various acceptable selected values.
      *
      * @return array

+ 11 - 0
tests/TestCase/View/Widget/SelectBoxWidgetTest.php

@@ -671,6 +671,17 @@ class SelectBoxWidgetTest extends TestCase
         ];
         $this->assertHtml($expected, $result);
 
+        $data['empty'] = ['99' => '(choose one)'];
+        $result = $select->render($data, $this->context);
+        $expected = [
+            'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
+            ['option' => ['value' => '99']], '(choose one)', '/option',
+            ['option' => ['value' => 'a']], 'Albatross', '/option',
+            ['option' => ['value' => 'b']], 'Budgie', '/option',
+            '/select'
+        ];
+        $this->assertHtml($expected, $result);
+
         $data['empty'] = 'empty';
         $data['val'] = '';
         $result = $select->render($data, $this->context);