Browse Source

More tests.

euromark 10 years ago
parent
commit
fb555906e5
2 changed files with 35 additions and 7 deletions
  1. 4 4
      Model/MyModel.php
  2. 31 3
      Test/Case/Model/MyModelTest.php

+ 4 - 4
Model/MyModel.php

@@ -194,7 +194,7 @@ class MyModel extends ShimModel {
 		if (!empty($customOptions['timestampField'])) {
 			$data[$customOptions['timestampField']] = date(FORMAT_DB_DATETIME);
 		}
-		$this->id = $id;
+		$data[$this->primaryKey] = $id;
 		return $this->save($data, false);
 	}
 
@@ -1310,12 +1310,12 @@ class MyModel extends ShimModel {
 	 * @return ARRAY record: [Model][values],...
 	 */
 	public function toggleField($fieldName, $id) {
-		$record = $this->get($id, ['conditions' => [$this->primaryKey, $fieldName]]);
+		$record = $this->get($id, ['fields' => [$this->primaryKey, $fieldName]]);
 
 		if (!empty($record) && !empty($fieldName) && $this->hasField($fieldName)) {
 			$record[$this->alias][$fieldName] = ($record[$this->alias][$fieldName] == 1 ? 0 : 1);
-			$this->id = $id;
-			$this->saveField($fieldName, $record[$this->alias][$fieldName]);
+			$data = [$fieldName => $record[$this->alias][$fieldName]];
+			$this->updateAllJoinless($data, [$this->primaryKey => $id]);
 		}
 		return $record;
 	}

+ 31 - 3
Test/Case/Model/MyModelTest.php

@@ -202,14 +202,42 @@ class MyModelTest extends MyCakeTestCase {
 	 */
 	public function testUpdate() {
 		$record = ['title' => 'x', 'body' => 'bx'];
-		$result = $this->User->save($record);
+		$result = $this->Post->save($record);
 		$this->assertTrue((bool)$result);
 
 		$record['body'] = 'bxx';
-		$result = $this->User->update($result['User']['id'], ['body' => $record['body']]);
+		$result = $this->Post->update($result['Post']['id'], ['body' => $record['body']]);
 		$this->assertTrue((bool)$result);
 
-		$this->assertSame($record['body'], $result['User']['body']);
+		$this->assertSame($record['body'], $result['Post']['body']);
+
+		$result = $this->Post->get($result['Post']['id']);
+		$this->assertSame($record['body'], $result['Post']['body']);
+	}
+
+	/**
+	 * @return void
+	 */
+	public function testToggleField() {
+		$record = ['title' => 'x', 'body' => 0];
+		$result = $this->Post->save($record);
+		$this->assertTrue((bool)$result);
+
+		$result = $this->Post->toggleField('body', $result['Post']['id']);
+		$this->assertTrue((bool)$result);
+
+		$this->assertSame(1, $result['Post']['body']);
+
+		$result = $this->Post->get($result['Post']['id']);
+		$this->assertSame('1', $result['Post']['body']);
+
+		$result = $this->Post->toggleField('body', $result['Post']['id']);
+		$this->assertTrue((bool)$result);
+
+		$this->assertSame(0, $result['Post']['body']);
+
+		$result = $this->Post->get($result['Post']['id']);
+		$this->assertSame('0', $result['Post']['body']);
 	}
 
 	/**