Skip to content

Commit

Permalink
Merge branch 'master' of github.com:cakephp/cakephp
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Apr 23, 2015
2 parents b0bf141 + e362481 commit acabf50
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 132 deletions.
12 changes: 6 additions & 6 deletions src/Controller/Component/RequestHandlerComponent.php
Expand Up @@ -31,7 +31,7 @@
* Request object for handling alternative HTTP requests
*
* Alternative HTTP requests can come from wireless units like mobile phones, palmtop computers,
* and the like. These units have no use for Ajax requests, and this Component can tell how Cake
* and the like. These units have no use for AJAX requests, and this Component can tell how Cake
* should respond to the different needs of a handheld computer and a desktop machine.
*
* @link http://book.cakephp.org/3.0/en/controllers/components/request-handling.html
Expand Down Expand Up @@ -142,7 +142,7 @@ public function implementedEvents()
* Checks to see if a specific content type has been requested and sets RequestHandler::$ext
* accordingly. Checks the following in order: 1. The '_ext' value parsed by the Router. 2. A specific
* AJAX type request indicated by the presence of a header. 3. The Accept header. With the exception
* of an ajax request indicated using the second header based method above, the type must have
* of an AJAX request indicated using the second header based method above, the type must have
* been configured in {@link Cake\Routing\Router}.
*
* @param array $config The config data.
Expand Down Expand Up @@ -273,12 +273,12 @@ public function convertXml($xml)
}

/**
* Handles (fakes) redirects for Ajax requests using requestAction()
* Handles (fakes) redirects for AJAX requests using requestAction()
*
* @param Event $event The Controller.beforeRedirect event.
* @param string|array $url A string or array containing the redirect location
* @param \Cake\Network\Response $response The response object.
* @return void
* @return void|\Cake\Network\Response The response object if the redirect is caught.
*/
public function beforeRedirect(Event $event, $url, Response $response)
{
Expand All @@ -300,8 +300,8 @@ public function beforeRedirect(Event $event, $url, Response $response)
'REQUEST_METHOD' => 'GET'
]
]));
$response->send();
$response->stop();
$response->statusCode(200);
return $response;
}

/**
Expand Down
11 changes: 9 additions & 2 deletions src/I18n/Time.php
Expand Up @@ -246,6 +246,8 @@ public function toUnixString()
*/
public function timeAgoInWords(array $options = [])
{
$time = $this;

$timezone = null;
$format = static::$wordFormat;
$end = static::$wordEnd;
Expand All @@ -272,8 +274,13 @@ public function timeAgoInWords(array $options = [])
}
}

if ($timezone) {
$time = clone $this;
$time->timezone($timezone);
}

$now = $from->format('U');
$inSeconds = $this->format('U');
$inSeconds = $time->format('U');
$backwards = ($inSeconds > $now);

$futureTime = $now;
Expand All @@ -289,7 +296,7 @@ public function timeAgoInWords(array $options = [])
}

if ($diff > abs($now - (new static($end))->format('U'))) {
return sprintf($absoluteString, $this->i18nFormat($format));
return sprintf($absoluteString, $time->i18nFormat($format));
}

// If more than a week, then take into account the length of months
Expand Down
5 changes: 2 additions & 3 deletions src/ORM/Marshaller.php
Expand Up @@ -437,9 +437,8 @@ public function merge(EntityInterface $entity, array $data, array $options = [])
foreach ((array)$options['fieldList'] as $field) {
if (array_key_exists($field, $properties)) {
$entity->set($field, $properties[$field]);
if ($properties[$field] instanceof EntityInterface &&
isset($marshalledAssocs[$field])) {
$entity->dirty($assoc, $properties[$field]->dirty());
if ($properties[$field] instanceof EntityInterface && isset($marshalledAssocs[$field])) {
$entity->dirty($field, $properties[$field]->dirty());
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/View/Widget/BasicWidget.php
Expand Up @@ -85,6 +85,9 @@ public function render(array $data, ContextInterface $context)
*/
public function secureFields(array $data)
{
if (!isset($data['name']) || $data['name'] === '') {
return [];
}
return [$data['name']];
}
}
32 changes: 2 additions & 30 deletions src/View/Widget/ButtonWidget.php
Expand Up @@ -15,7 +15,7 @@
namespace Cake\View\Widget;

use Cake\View\Form\ContextInterface;
use Cake\View\Widget\WidgetInterface;
use Cake\View\Widget\BasicWidget;

/**
* Button input class
Expand All @@ -24,26 +24,9 @@
* If you need to make basic submit inputs with type=submit,
* use the Basic input widget.
*/
class ButtonWidget implements WidgetInterface
class ButtonWidget extends BasicWidget
{

/**
* StringTemplate instance.
*
* @var \Cake\View\StringTemplate
*/
protected $_templates;

/**
* Constructor.
*
* @param \Cake\View\StringTemplate $templates Templates list.
*/
public function __construct($templates)
{
$this->_templates = $templates;
}

/**
* Render a button.
*
Expand Down Expand Up @@ -72,15 +55,4 @@ public function render(array $data, ContextInterface $context)
'attrs' => $this->_templates->formatAttributes($data, ['text']),
]);
}

/**
* {@inheritDoc}
*/
public function secureFields(array $data)
{
if (!isset($data['name'])) {
return [];
}
return [$data['name']];
}
}
29 changes: 2 additions & 27 deletions src/View/Widget/CheckboxWidget.php
Expand Up @@ -15,31 +15,14 @@
namespace Cake\View\Widget;

use Cake\View\Form\ContextInterface;
use Cake\View\Widget\WidgetInterface;
use Cake\View\Widget\BasicWidget;

/**
* Input widget for creating checkbox widgets.
*/
class CheckboxWidget implements WidgetInterface
class CheckboxWidget extends BasicWidget
{

/**
* Template instance.
*
* @var \Cake\View\StringTemplate
*/
protected $_templates;

/**
* Constructor
*
* @param \Cake\View\StringTemplate $templates Templates list.
*/
public function __construct($templates)
{
$this->_templates = $templates;
}

/**
* Render a checkbox element.
*
Expand Down Expand Up @@ -98,12 +81,4 @@ protected function _isChecked($data)
}
return false;
}

/**
* {@inheritDoc}
*/
public function secureFields(array $data)
{
return [$data['name']];
}
}
29 changes: 2 additions & 27 deletions src/View/Widget/SelectBoxWidget.php
Expand Up @@ -15,7 +15,7 @@
namespace Cake\View\Widget;

use Cake\View\Form\ContextInterface;
use Cake\View\Widget\WidgetInterface;
use Cake\View\Widget\BasicWidget;
use Traversable;

/**
Expand All @@ -24,26 +24,9 @@
* This class is intended as an internal implementation detail
* of Cake\View\Helper\FormHelper and is not intended for direct use.
*/
class SelectBoxWidget implements WidgetInterface
class SelectBoxWidget extends BasicWidget
{

/**
* Template instance.
*
* @var \Cake\View\StringTemplate
*/
protected $_templates;

/**
* Constructor
*
* @param \Cake\View\StringTemplate $templates Templates list.
*/
public function __construct($templates)
{
$this->_templates = $templates;
}

/**
* Render a select box form input.
*
Expand Down Expand Up @@ -292,12 +275,4 @@ protected function _isDisabled($key, $disabled)
$strict = !is_numeric($key);
return in_array((string)$key, $disabled, $strict);
}

/**
* {@inheritDoc}
*/
public function secureFields(array $data)
{
return [$data['name']];
}
}
23 changes: 2 additions & 21 deletions src/View/Widget/TextareaWidget.php
Expand Up @@ -15,27 +15,16 @@
namespace Cake\View\Widget;

use Cake\View\Form\ContextInterface;
use Cake\View\Widget\WidgetInterface;
use Cake\View\Widget\BasicWidget;

/**
* Input widget class for generating a textarea control.
*
* This class is intended as an internal implementation detail
* of Cake\View\Helper\FormHelper and is not intended for direct use.
*/
class TextareaWidget implements WidgetInterface
class TextareaWidget extends BasicWidget
{

/**
* Constructor
*
* @param \Cake\View\StringTemplate $templates Templates list.
*/
public function __construct($templates)
{
$this->_templates = $templates;
}

/**
* Render a text area form widget.
*
Expand Down Expand Up @@ -68,12 +57,4 @@ public function render(array $data, ContextInterface $context)
)
]);
}

/**
* {@inheritDoc}
*/
public function secureFields(array $data)
{
return [$data['name']];
}
}
53 changes: 38 additions & 15 deletions tests/TestCase/Controller/Component/RequestHandlerComponentTest.php
Expand Up @@ -793,7 +793,7 @@ public function testPrefers()
}

/**
* test that ajax requests involving redirects trigger requestAction instead.
* test that AJAX requests involving redirects trigger requestAction instead.
*
* @return void
* @triggers Controller.beforeRedirect $this->Controller
Expand All @@ -810,16 +810,42 @@ public function testAjaxRedirectAsRequestAction()
$this->Controller->RequestHandler->request = $this->Controller->request;
$this->Controller->RequestHandler->response = $this->Controller->response;
$this->Controller->request->expects($this->any())->method('is')->will($this->returnValue(true));
$this->Controller->response->expects($this->once())->method('stop');

ob_start();
$this->Controller->RequestHandler->beforeRedirect(
$response = $this->Controller->RequestHandler->beforeRedirect(
$event,
['controller' => 'request_handler_test', 'action' => 'destination'],
['controller' => 'RequestHandlerTest', 'action' => 'destination'],
$this->Controller->response
);
$result = ob_get_clean();
$this->assertRegExp('/posts index/', $result, 'RequestAction redirect failed.');
$this->assertRegExp('/posts index/', $response->body(), 'RequestAction redirect failed.');
}

/**
* Tests that AJAX requests involving redirects don't let the status code bleed through.
*
* @return void
* @triggers Controller.beforeRedirect $this->Controller
*/
public function testAjaxRedirectAsRequestActionStatusCode()
{
Configure::write('App.namespace', 'TestApp');
Router::connect('/:controller/:action');
$event = new Event('Controller.beforeRedirect', $this->Controller);

$this->Controller->RequestHandler = new RequestHandlerComponent($this->Controller->components());
$this->Controller->request = $this->getMock('Cake\Network\Request', ['is']);
$this->Controller->response = $this->getMock('Cake\Network\Response', ['_sendHeader', 'stop']);
$this->Controller->response->statusCode(302);
$this->Controller->RequestHandler->request = $this->Controller->request;
$this->Controller->RequestHandler->response = $this->Controller->response;
$this->Controller->request->expects($this->any())->method('is')->will($this->returnValue(true));

$response = $this->Controller->RequestHandler->beforeRedirect(
$event,
['controller' => 'RequestHandlerTest', 'action' => 'destination'],
$this->Controller->response
);
$this->assertRegExp('/posts index/', $response->body(), 'RequestAction redirect failed.');
$this->assertSame(200, $response->statusCode());
}

/**
Expand All @@ -841,17 +867,14 @@ public function testAjaxRedirectAsRequestActionStillRenderingLayout()
$this->Controller->RequestHandler->request = $this->Controller->request;
$this->Controller->RequestHandler->response = $this->Controller->response;
$this->Controller->request->expects($this->any())->method('is')->will($this->returnValue(true));
$this->Controller->response->expects($this->once())->method('stop');

ob_start();
$this->Controller->RequestHandler->beforeRedirect(
$response = $this->Controller->RequestHandler->beforeRedirect(
$event,
['controller' => 'request_handler_test', 'action' => 'ajax2_layout'],
['controller' => 'RequestHandlerTest', 'action' => 'ajax2_layout'],
$this->Controller->response
);
$result = ob_get_clean();
$this->assertRegExp('/posts index/', $result, 'RequestAction redirect failed.');
$this->assertRegExp('/Ajax!/', $result, 'Layout was not rendered.');
$this->assertRegExp('/posts index/', $response->body(), 'RequestAction redirect failed.');
$this->assertRegExp('/Ajax!/', $response->body(), 'Layout was not rendered.');
}

/**
Expand Down Expand Up @@ -882,7 +905,7 @@ public function testBeforeRedirectCallbackWithArrayUrl()
ob_start();
$RequestHandler->beforeRedirect(
$event,
['controller' => 'request_handler_test', 'action' => 'param_method', 'first', 'second'],
['controller' => 'RequestHandlerTest', 'action' => 'param_method', 'first', 'second'],
$this->Controller->response
);
$result = ob_get_clean();
Expand Down

0 comments on commit acabf50

Please sign in to comment.