Browse Source

Simply flash message setting in auth component.

ADmad 11 years ago
parent
commit
c3a48581ba

+ 9 - 18
src/Controller/Component/AuthComponent.php

@@ -105,8 +105,10 @@ class AuthComponent extends Component {
  * - `flash` - Settings to use when Auth needs to do a flash message with
  *   FlashComponent::set(). Available keys are:
  *
- *   - `key` - The message domain to use for flashes generated by this component, defaults to 'auth'.
- *   - `params` - The array of additional params to use, defaults to []
+ *   - `key` - The message domain to use for flashes generated by this component,
+ *     defaults to 'auth'.
+ *   - `element` - Flash element to use, defaults to 'default'.
+ *   - `params` - The array of additional params to use, defaults to ['class' => 'error']
  *
  * - `loginAction` - A URL (defined as a string or array) to the controller action
  *   that handles logins. Defaults to `/users/login`.
@@ -422,7 +424,7 @@ class AuthComponent extends Component {
 			'flash' => [
 				'element' => 'default',
 				'key' => 'auth',
-				'params' => []
+				'params' => ['class' => 'error']
 			],
 			'loginAction' => [
 				'controller' => 'Users',
@@ -785,26 +787,15 @@ class AuthComponent extends Component {
 	}
 
 /**
- * Set a flash message. Uses the Session component, and values from `flash` config.
+ * Set a flash message. Uses the Flash component with values from `flash` config.
  *
  * @param string $message The message to set.
- * @param string $type Message type. Defaults to 'error'.
  * @return void
  */
-	public function flash($message, $type = 'error') {
-		if ($message === false) {
-			return;
-		}
-		$flashConfig = $this->_config['flash'];
-		$key = $flashConfig['key'];
-		$params = [];
-		if (isset($flashConfig['params'])) {
-			$params = $flashConfig['params'];
-		}
-		if (empty($params['element'])) {
-			$params['element'] = 'error';
+	public function flash($message) {
+		if ($message !== false) {
+			$this->Flash->set($message, $this->_config['flash']);
 		}
-		$this->Flash->set($message, $params + compact('key'));
 	}
 
 /**

+ 15 - 4
tests/TestCase/Controller/Component/AuthComponentTest.php

@@ -1098,22 +1098,33 @@ class AuthComponentTest extends TestCase {
 			[],
 			[$this->Controller->components()]
 		);
+		$this->Controller->methods = ['foo'];
+		$this->Controller->request->params['action'] = 'foo';
+		$this->Auth->startup(new Event('Controller.startup', $this->Controller));
+
 		$this->Auth->Flash->expects($this->at(0))
 			->method('set')
-			->with('Auth failure', array('key' => 'auth-key', 'element' => 'custom'));
+			->with(
+				'Auth failure',
+				[
+					'key' => 'auth-key',
+					'element' => 'default',
+					'params' => ['class' => 'error']
+				]
+			);
 
 		$this->Auth->Flash->expects($this->at(1))
 			->method('set')
-			->with('Auth failure', array('element' => 'error', 'key' => 'auth-key'));
+			->with('Auth failure', ['key' => 'auth-key', 'element' => 'custom']);
 
 		$this->Auth->config('flash', [
-			'params' => array('element' => 'custom'),
 			'key' => 'auth-key'
 		]);
 		$this->Auth->flash('Auth failure');
 
 		$this->Auth->config('flash', [
-			'key' => 'auth-key'
+			'key' => 'auth-key',
+			'element' => 'custom'
 		], false);
 		$this->Auth->flash('Auth failure');
 	}