|
|
@@ -21,6 +21,8 @@ use Cake\Routing\DispatcherFactory;
|
|
|
use Cake\Routing\Router;
|
|
|
use Cake\TestSuite\Stub\Response;
|
|
|
use Cake\Utility\Hash;
|
|
|
+use Cake\Utility\Text;
|
|
|
+use Cake\View\Helper\SecureFieldTokenTrait;
|
|
|
use Exception;
|
|
|
use PHPUnit_Exception;
|
|
|
|
|
|
@@ -36,6 +38,7 @@ use PHPUnit_Exception;
|
|
|
*/
|
|
|
abstract class IntegrationTestCase extends TestCase
|
|
|
{
|
|
|
+ use SecureFieldTokenTrait;
|
|
|
|
|
|
/**
|
|
|
* The data used to build the next request.
|
|
|
@@ -101,6 +104,22 @@ abstract class IntegrationTestCase extends TestCase
|
|
|
protected $_requestSession;
|
|
|
|
|
|
/**
|
|
|
+ * Boolean flag for whether or not the request should have
|
|
|
+ * a SecurityComponent token added.
|
|
|
+ *
|
|
|
+ * @var bool
|
|
|
+ */
|
|
|
+ protected $_securityToken = false;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Boolean flag for whether or not the request should have
|
|
|
+ * a CSRF token added.
|
|
|
+ *
|
|
|
+ * @var bool
|
|
|
+ */
|
|
|
+ protected $_csrfToken = false;
|
|
|
+
|
|
|
+ /**
|
|
|
* Clears the state used for requests.
|
|
|
*
|
|
|
* @return void
|
|
|
@@ -117,6 +136,33 @@ abstract class IntegrationTestCase extends TestCase
|
|
|
$this->_viewName = null;
|
|
|
$this->_layoutName = null;
|
|
|
$this->_requestSession = null;
|
|
|
+ $this->_securityToken = false;
|
|
|
+ $this->_csrfToken = false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Calling this method will enable a SecurityComponent
|
|
|
+ * compatible token to be added to request data. This
|
|
|
+ * lets you easily test actions protected by SecurityComponent.
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function enableSecurityToken()
|
|
|
+ {
|
|
|
+ $this->_securityToken = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Calling this method will add a CSRF token to the request.
|
|
|
+ *
|
|
|
+ * Both the POST data and cookie will be populated when this option
|
|
|
+ * is enabled. The default parameter names will be used.
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function enableCsrfToken()
|
|
|
+ {
|
|
|
+ $this->_csrfToken = true;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -343,11 +389,11 @@ abstract class IntegrationTestCase extends TestCase
|
|
|
];
|
|
|
$session = Session::create($sessionConfig);
|
|
|
$session->write($this->_session);
|
|
|
-
|
|
|
list ($url, $query) = $this->_url($url);
|
|
|
+
|
|
|
$props = [
|
|
|
'url' => $url,
|
|
|
- 'post' => $data,
|
|
|
+ 'post' => $this->_addTokens($url, $data),
|
|
|
'cookies' => $this->_cookie,
|
|
|
'session' => $session,
|
|
|
'query' => $query
|
|
|
@@ -369,6 +415,33 @@ abstract class IntegrationTestCase extends TestCase
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * Add the CSRF and Security Component tokens if necessary.
|
|
|
+ *
|
|
|
+ * @param string $url The URL the form is being submitted on.
|
|
|
+ * @param array $data The request body data.
|
|
|
+ * @return array The request body with tokens added.
|
|
|
+ */
|
|
|
+ protected function _addTokens($url, $data)
|
|
|
+ {
|
|
|
+ if ($this->_securityToken === true) {
|
|
|
+ $keys = Hash::flatten($data);
|
|
|
+ $tokenData = $this->_buildFieldToken($url, $keys);
|
|
|
+ $data['_Token'] = $tokenData;
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($this->_csrfToken === true) {
|
|
|
+ $csrfToken = Text::uuid();
|
|
|
+ if (!isset($data['_csrfToken'])) {
|
|
|
+ $data['_csrfToken'] = $csrfToken;
|
|
|
+ }
|
|
|
+ if (!isset($this->_cookie['csrfToken'])) {
|
|
|
+ $this->_cookie['csrfToken'] = $csrfToken;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return $data;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* Creates a valid request url and parameter array more like Request::_url()
|
|
|
*
|
|
|
* @param string|array $url The URL
|