Browse Source

Merge branch '3.0' of git://github.com/cakephp/cakephp into feature/3.0-get-tree-level

Florian Krämer 11 years ago
parent
commit
9674065f0b

+ 2 - 1
src/Auth/ControllerAuthorize.php

@@ -25,7 +25,8 @@ use Cake\Network\Request;
  * return a boolean to indicate whether or not the user is authorized.
  *
  * ```
- *  public function isAuthorized($user) {
+ *  public function isAuthorized($user)
+ *  {
  *      if ($this->request->param('admin')) {
  *          return $user['role'] === 'admin';
  *      }

+ 3 - 3
src/Controller/Controller.php

@@ -497,16 +497,16 @@ class Controller implements EventListenerInterface
      *
      * @param string|array $url A string or array-based URL pointing to another location within the app,
      *     or an absolute URL
-     * @param int $status Optional HTTP status code (eg: 404)
+     * @param int $status HTTP status code (eg: 301)
      * @return void|\Cake\Network\Response
      * @link http://book.cakephp.org/3.0/en/controllers.html#Controller::redirect
      */
-    public function redirect($url, $status = null)
+    public function redirect($url, $status = 302)
     {
         $this->autoRender = false;
 
         $response = $this->response;
-        if ($status && $response->statusCode() === 200) {
+        if ($status) {
             $response->statusCode($status);
         }
 

+ 2 - 1
src/Database/Driver/Sqlserver.php

@@ -15,6 +15,7 @@
 namespace Cake\Database\Driver;
 
 use Cake\Database\Dialect\SqlserverDialectTrait;
+use Cake\Database\Query;
 use Cake\Database\Statement\SqlserverStatement;
 use PDO;
 
@@ -105,7 +106,7 @@ class Sqlserver extends \Cake\Database\Driver
         if ($isObject && $query->bufferResults() === false) {
             $options = [];
         }
-        $statement = $this->_connection->prepare($isObject ? $query->sql() : $query);
+        $statement = $this->_connection->prepare($isObject ? $query->sql() : $query, $options);
         return new SqlserverStatement($statement, $this);
     }
 }

+ 2 - 1
src/Event/EventListenerInterface.php

@@ -29,7 +29,8 @@ interface EventListenerInterface
      * ### Example:
      *
      * ```
-     *  public function implementedEvents() {
+     *  public function implementedEvents()
+     *  {
      *      return [
      *          'Order.complete' => 'sendEmail',
      *          'Article.afterBuy' => 'decrementInventory',

+ 4 - 2
src/Event/README.md

@@ -15,11 +15,13 @@ of the action.
 use Cake\Event\Event;
 use Cake\Event\EventManagerTrait;
 
-class Orders {
+class Orders
+{
 
 	use EventManagerTrait;
 
-	public function placeOrder($order) {
+	public function placeOrder($order)
+	{
 		$this->doStuff();
 		$event = new Event('Orders.afterPlace', $this, [
 			'order' => $order

+ 9 - 4
src/ORM/Table.php

@@ -273,7 +273,8 @@ class Table implements RepositoryInterface, EventListenerInterface
      * define validation and do any other initialization logic you need.
      *
      * ```
-     *  public function initialize(array $config) {
+     *  public function initialize(array $config)
+     *  {
      *      $this->belongsTo('Users');
      *      $this->belongsToMany('Tagging.Tags');
      *      $this->primaryKey('something_else');
@@ -1100,7 +1101,8 @@ class Table implements RepositoryInterface, EventListenerInterface
      * you will need to create a method in your Table subclass as follows:
      *
      * ```
-     * public function validationForSubscription($validator) {
+     * public function validationForSubscription($validator)
+     * {
      *  return $validator
      *  ->add('email', 'valid-email', ['rule' => 'email'])
      *  ->add('password', 'valid', ['rule' => 'notEmpty'])
@@ -1203,6 +1205,8 @@ class Table implements RepositoryInterface, EventListenerInterface
      *   to be saved. It is possible to provide different options for saving on associated
      *   table objects using this key by making the custom options the array value.
      *   If false no associated records will be saved. (default: true)
+     * - checkExisting: Whether or not to check if the entity already exists, assuming that the
+     *   entity is marked as not new, and the primary key has been set.
      *
      * ### Events
      *
@@ -1268,7 +1272,8 @@ class Table implements RepositoryInterface, EventListenerInterface
         $options = new ArrayObject($options + [
             'atomic' => true,
             'associated' => true,
-            'checkRules' => true
+            'checkRules' => true,
+            'checkExisting' => true
         ]);
 
         if ($entity->errors()) {
@@ -1303,7 +1308,7 @@ class Table implements RepositoryInterface, EventListenerInterface
     {
         $primaryColumns = (array)$this->primaryKey();
 
-        if ($primaryColumns && $entity->isNew() && $entity->has($primaryColumns)) {
+        if ($options['checkExisting'] && $primaryColumns && $entity->isNew() && $entity->has($primaryColumns)) {
             $alias = $this->alias();
             $conditions = [];
             foreach ($entity->extract($primaryColumns) as $k => $v) {

+ 1 - 1
src/Validation/Validator.php

@@ -354,7 +354,7 @@ class Validator implements \ArrayAccess, \IteratorAggregate, \Countable
      * ### Example:
      *
      * ```
-     * $validator->allowEmpty('email'); // Email cannot be empty
+     * $validator->allowEmpty('email'); // Email can be empty
      * $validator->allowEmpty('email', 'create'); // Email can be empty on create
      * $validator->allowEmpty('email', 'update'); // Email can be empty on update
      * ```

+ 2 - 2
tests/TestCase/ORM/RulesCheckerIntegrationTest.php

@@ -414,7 +414,7 @@ class RulesCheckerIntegrationTest extends TestCase
         $table->eventManager()->attach(
             function ($event, Entity $entity, \ArrayObject $options, $operation) {
                 $this->assertEquals(
-                    ['atomic' => true, 'associated' => true, 'checkRules' => true],
+                    ['atomic' => true, 'associated' => true, 'checkRules' => true, 'checkExisting' => true],
                     $options->getArrayCopy()
                 );
                 $this->assertEquals('create', $operation);
@@ -447,7 +447,7 @@ class RulesCheckerIntegrationTest extends TestCase
         $table->eventManager()->attach(
             function ($event, Entity $entity, \ArrayObject $options, $result, $operation) {
                 $this->assertEquals(
-                    ['atomic' => true, 'associated' => true, 'checkRules' => true],
+                    ['atomic' => true, 'associated' => true, 'checkRules' => true, 'checkExisting' => true],
                     $options->getArrayCopy()
                 );
                 $this->assertEquals('create', $operation);

+ 60 - 0
tests/TestCase/ORM/TableTest.php

@@ -1369,6 +1369,66 @@ class TableTest extends TestCase
     }
 
     /**
+     * Test that saving a new entity with a Primary Key set does call exists.
+     *
+     * @group save
+     * @return void
+     */
+    public function testSavePrimaryKeyEntityExists()
+    {
+        $this->skipIf(
+            $this->connection->driver() instanceof \Cake\Database\Driver\Sqlserver,
+            'SQLServer does not like setting an id on IDENTITY fields'
+        );
+        $table = $this->getMock(
+            'Cake\ORM\Table',
+            ['exists'],
+            [
+                [
+                    'connection' => $this->connection,
+                    'alias' => 'Users',
+                    'table' => 'users',
+                ]
+            ]
+        );
+        $entity = $table->newEntity(['id' => 20, 'username' => 'mark']);
+        $this->assertTrue($entity->isNew());
+
+        $table->expects($this->once())->method('exists');
+        $this->assertSame($entity, $table->save($entity));
+    }
+
+    /**
+     * Test that saving a new entity with a Primary Key set does not call exists when checkExisting is false.
+     *
+     * @group save
+     * @return void
+     */
+    public function testSavePrimaryKeyEntityNoExists()
+    {
+        $this->skipIf(
+            $this->connection->driver() instanceof \Cake\Database\Driver\Sqlserver,
+            'SQLServer does not like setting an id on IDENTITY fields'
+        );
+        $table = $this->getMock(
+            'Cake\ORM\Table',
+            ['exists'],
+            [
+                [
+                    'connection' => $this->connection,
+                    'alias' => 'Users',
+                    'table' => 'users',
+                ]
+            ]
+        );
+        $entity = $table->newEntity(['id' => 20, 'username' => 'mark']);
+        $this->assertTrue($entity->isNew());
+
+        $table->expects($this->never())->method('exists');
+        $this->assertSame($entity, $table->save($entity, ['checkExisting' => false]));
+    }
+
+    /**
      * Tests that saving an entity will filter out properties that
      * are not present in the table schema when saving
      *