Browse Source

Merge branch 'master' into 3.next

Mark Story 9 years ago
parent
commit
9b3a191f66

+ 11 - 2
src/Controller/Component/FlashComponent.php

@@ -47,7 +47,8 @@ class FlashComponent extends Component
         'key' => 'flash',
         'element' => 'default',
         'params' => [],
-        'clear' => false
+        'clear' => false,
+        'duplicate' => true
     ];
 
     /**
@@ -107,10 +108,18 @@ class FlashComponent extends Component
         }
 
         $messages = [];
-        if ($options['clear'] === false) {
+        if (!$options['clear']) {
             $messages = (array)$this->_session->read('Flash.' . $options['key']);
         }
 
+        if (!$options['duplicate']) {
+            foreach ($messages as $existingMessage) {
+                if ($existingMessage['message'] === $message) {
+                    return;
+                }
+            }
+        }
+
         $messages[] = [
             'message' => $message,
             'key' => $options['key'],

+ 18 - 12
src/Controller/Component/PaginatorComponent.php

@@ -287,25 +287,31 @@ class PaginatorComponent extends Component
     }
 
     /**
-     * Get the default settings for a $model. If there are no settings for a specific model, the general settings
+     * Get the settings for a $model. If there are no settings for a specific model, the general settings
      * will be used.
      *
-     * @param string $alias Model name to get default settings for.
-     * @param array $defaults The defaults to use for combining settings.
-     * @return array An array of pagination defaults for a model, or the general settings.
+     * @param string $alias Model name to get settings for.
+     * @param array $settings The settings which is used for combining.
+     * @return array An array of pagination settings for a model, or the general settings.
      */
-    public function getDefaults($alias, $defaults)
+    public function getDefaults($alias, $settings)
     {
-        if (isset($defaults[$alias])) {
-            $defaults = $defaults[$alias];
+        if (isset($settings[$alias])) {
+            $settings = $settings[$alias];
         }
-        if (isset($defaults['limit']) &&
-            (empty($defaults['maxLimit']) || $defaults['limit'] > $defaults['maxLimit'])
-        ) {
-            $defaults['maxLimit'] = $defaults['limit'];
+
+        $defaults = $this->config();
+        $maxLimit = isset($settings['maxLimit']) ? $settings['maxLimit'] : $defaults['maxLimit'];
+        $limit = isset($settings['limit']) ? $settings['limit'] : $defaults['limit'];
+
+        if ($limit > $maxLimit) {
+            $limit = $maxLimit;
         }
 
-        return $defaults + $this->config();
+        $settings['maxLimit'] = $maxLimit;
+        $settings['limit'] = $limit;
+
+        return $settings + $defaults;
     }
 
     /**

+ 30 - 0
src/TestSuite/IntegrationTestCase.php

@@ -868,6 +868,36 @@ abstract class IntegrationTestCase extends TestCase
     }
 
     /**
+     * Asserts that the response body matches a given regular expression.
+     *
+     * @param string $pattern The pattern to compare against.
+     * @param string $message The failure message that will be appended to the generated message.
+     * @return void
+     */
+    public function assertResponseRegExp($pattern, $message = '')
+    {
+        if (!$this->_response) {
+            $this->fail('No response set, cannot assert content. ' . $message);
+        }
+        $this->assertRegExp($pattern, (string)$this->_response->body(), $message);
+    }
+
+    /**
+     * Asserts that the response body does not match a given regular expression.
+     *
+     * @param string $pattern The pattern to compare against.
+     * @param string $message The failure message that will be appended to the generated message.
+     * @return void
+     */
+    public function assertResponseNotRegExp($pattern, $message = '')
+    {
+        if (!$this->_response) {
+            $this->fail('No response set, cannot assert content. ' . $message);
+        }
+        $this->assertNotRegExp($pattern, (string)$this->_response->body(), $message);
+    }
+
+    /**
      * Assert response content is not empty.
      *
      * @param string $message The failure message that will be appended to the generated message.

+ 11 - 0
tests/TestCase/Controller/Component/FlashComponentTest.php

@@ -110,6 +110,17 @@ class FlashComponentTest extends TestCase
         $this->assertEquals($expected, $result);
     }
 
+    public function testDuplicateIgnored()
+    {
+        $this->assertNull($this->Session->read('Flash.flash'));
+
+        $this->Flash->config('duplicate', false);
+        $this->Flash->set('This test message should appear once only');
+        $this->Flash->set('This test message should appear once only');
+        $result = $this->Session->read('Flash.flash');
+        $this->assertCount(1, $result);
+    }
+
     /**
      * @return void
      */

+ 51 - 3
tests/TestCase/Controller/Component/PaginatorComponentTest.php

@@ -480,8 +480,8 @@ class PaginatorComponentTest extends TestCase
         $result = $this->Paginator->mergeOptions('Post', $settings);
         $expected = [
             'page' => 1,
-            'limit' => 200,
-            'maxLimit' => 200,
+            'limit' => 100,
+            'maxLimit' => 100,
             'paramType' => 'named',
             'whitelist' => ['limit', 'sort', 'page', 'direction']
         ];
@@ -494,7 +494,7 @@ class PaginatorComponentTest extends TestCase
         $result = $this->Paginator->mergeOptions('Post', $settings);
         $expected = [
             'page' => 1,
-            'limit' => 20,
+            'limit' => 10,
             'maxLimit' => 10,
             'paramType' => 'named',
             'whitelist' => ['limit', 'sort', 'page', 'direction']
@@ -503,6 +503,54 @@ class PaginatorComponentTest extends TestCase
     }
 
     /**
+     * test getDefaults with limit > maxLimit in code.
+     *
+     * @return void
+     */
+    public function testGetDefaultMaxLimit()
+    {
+        $settings = [
+            'page' => 1,
+            'limit' => 2,
+            'maxLimit' => 10,
+            'order' => [
+                'Users.username' => 'asc'
+            ],
+        ];
+        $result = $this->Paginator->mergeOptions('Post', $settings);
+        $expected = [
+            'page' => 1,
+            'limit' => 2,
+            'maxLimit' => 10,
+            'order' => [
+                'Users.username' => 'asc'
+            ],
+            'whitelist' => ['limit', 'sort', 'page', 'direction']
+        ];
+        $this->assertEquals($expected, $result);
+
+        $settings = [
+            'page' => 1,
+            'limit' => 100,
+            'maxLimit' => 10,
+            'order' => [
+                'Users.username' => 'asc'
+            ],
+        ];
+        $result = $this->Paginator->mergeOptions('Post', $settings);
+        $expected = [
+            'page' => 1,
+            'limit' => 10,
+            'maxLimit' => 10,
+            'order' => [
+                'Users.username' => 'asc'
+            ],
+            'whitelist' => ['limit', 'sort', 'page', 'direction']
+        ];
+        $this->assertEquals($expected, $result);
+    }
+
+    /**
      * Integration test to ensure that validateSort is being used by paginate()
      *
      * @return void

+ 26 - 0
tests/TestCase/TestSuite/IntegrationTestCaseTest.php

@@ -750,6 +750,32 @@ class IntegrationTestCaseTest extends IntegrationTestCase
     }
 
     /**
+     * Test the content regexp assertion.
+     *
+     * @return void
+     */
+    public function testAssertResponseRegExp()
+    {
+        $this->_response = new Response();
+        $this->_response->body('Some content');
+
+        $this->assertResponseRegExp('/cont/');
+    }
+
+    /**
+     * Test the negated content regexp assertion.
+     *
+     * @return void
+     */
+    public function testAssertResponseNotRegExp()
+    {
+        $this->_response = new Response();
+        $this->_response->body('Some content');
+
+        $this->assertResponseNotRegExp('/cant/');
+    }
+
+    /**
      * Test that works in tandem with testEventManagerReset2 to
      * test the EventManager reset.
      *