Browse Source

Forward port fixes to 2.x.

dereuromark 9 years ago
parent
commit
4a89d0e7c6

+ 10 - 5
src/Model/Behavior/ResetBehavior.php

@@ -82,7 +82,7 @@ class ResetBehavior extends Behavior {
 	 * @param array $params
 	 * @return int Modified records
 	 */
-	public function resetRecords($params = []) {
+	public function resetRecords(array $params = []) {
 		$defaults = [
 			'page' => 1,
 			'limit' => $this->_config['limit'],
@@ -105,9 +105,11 @@ class ResetBehavior extends Behavior {
 		}
 		if (!$this->_config['updateTimestamp']) {
 			$fields = ['modified', 'updated'];
+			$updateFields = [];
 			foreach ($fields as $field) {
 				if ($this->_table->schema()->column($field)) {
 					$defaults['fields'][] = $field;
+					$updateFields[] = $field;
 					break;
 				}
 			}
@@ -115,17 +117,20 @@ class ResetBehavior extends Behavior {
 
 		$params += $defaults;
 		$count = $this->_table->find('count', compact('conditions'));
-		$max = ini_get('max_execution_time');
+		$max = (int)ini_get('max_execution_time');
 		if ($max) {
-			set_time_limit(max($max, $count / $this->_config['limit']));
+			set_time_limit(max($max, $count));
 		}
 
 		$modified = 0;
 		while (($records = $this->_table->find('all', $params)->toArray())) {
 			foreach ($records as $record) {
 				$fieldList = $params['fields'];
-				if (!empty($updateFields)) {
-					$fieldList = $updateFields;
+				if ($this->config('updateFields')) {
+					$fieldList = $this->config('updateFields');
+					if (!$this->_config['updateTimestamp']) {
+						$fieldList = array_merge($updateFields, $fieldList);
+					}
 				}
 				if ($fieldList && !in_array($this->_table->primaryKey(), $fieldList)) {
 					$fieldList[] = $this->_table->primaryKey();

+ 18 - 4
tests/TestCase/Model/Behavior/ResetBehaviorTest.php

@@ -29,8 +29,6 @@ class ResetBehaviorTest extends TestCase {
 
 		Configure::write('App.namespace', 'TestApp');
 
-		//set_time_limit(10);
-
 		$this->Table = TableRegistry::get('ResetComments');
 		$this->Table->addBehavior('Tools.Reset');
 	}
@@ -42,11 +40,27 @@ class ResetBehaviorTest extends TestCase {
 	}
 
 	/**
-	 * ResetBehaviorTest::testResetRecords()
-	 *
 	 * @return void
 	 */
 	public function testResetRecords() {
+		$x = $this->Table->find('all', ['fields' => ['comment', 'updated'], 'order' => ['updated' => 'DESC']])->first();
+		$x['updated'] = (string)$x['updated'];
+
+		$result = $this->Table->resetRecords();
+		$this->assertTrue((bool)$result);
+
+		$y = $this->Table->find('all', ['fields' => ['comment', 'updated'], 'order' => ['updated' => 'DESC']])->first();
+		$y['updated'] = (string)$y['updated'];
+		$this->assertSame($x->toArray(), $y->toArray());
+	}
+
+	/**
+	 * @return void
+	 */
+	public function testResetRecordsUpdateField() {
+		$this->Table->removeBehavior('Reset');
+		$this->Table->addBehavior('Tools.Reset', ['fields' => ['comment'], 'updateFields' => ['comment']]);
+
 		$x = $this->Table->find('all', ['fields' => ['comment'], 'order' => ['updated' => 'DESC']])->first();
 
 		$result = $this->Table->resetRecords();