Skip to content
Browse files

all tests passes

  • Loading branch information...
1 parent 42aae55 commit 46912c3fb7d82a1a0a7cb0aa62cd8c0793182bb7 @jippi jippi committed with AD7six Apr 17, 2012
Showing with 105 additions and 26 deletions.
  1. +85 −19 Case/Controller/Component/CrudComponentTest.php
  2. +20 −7 Controller/Component/CrudComponent.php
View
104 Case/Controller/Component/CrudComponentTest.php
@@ -15,6 +15,20 @@
/**
* Test visibility wrapper
*/
+ public function testGetSubject($additional = array()) {
+ return $this->getSubject($additional);
+ }
+
+ /**
+ * Test visibility wrapper
+ */
+ public function testRedirect($subject, $url = null) {
+ return $this->redirect($subject, $url);
+ }
+
+ /**
+ * Test visibility wrapper
+ */
public function testValidateId($id) {
return $this->validateId($id);
}
@@ -35,23 +49,32 @@ public function setUp() {
parent::setUp();
$Collection = new ComponentCollection();
- $settings = array(
- 'actions' => array(
- 'index',
- 'add',
- 'edit',
- 'view',
- 'delete'
- )
- );
- $this->Crud = new TestCrudComponent($Collection, $settings);
-
- $request = new CakeRequest();
- $response = new CakeResponse();
- $controller = new Controller($request, $response); //$this->getMock('Controller');
- $controller->name = 'CrudTest';
- $controller->methods = array();
- $this->Crud->initialize($controller);
+ $settings = array(
+ 'actions' => array(
+ 'index',
+ 'add',
+ 'edit',
+ 'view',
+ 'delete'
+ )
+ );
+
+ $this->Crud = $this->getMock(
+ 'TestCrudComponent',
+ null,
+ array($Collection, $settings)
+ );
+
+ $controller = $this->getMock('Controller', array('header', '_stop', 'redirect'), array(), "", false);
+ $controller->name = 'Examples';
+
+ $request = new CakeRequest();
+ $response = new CakeResponse();
+
+ $controller->__construct($request, $response);
+
+ $controller->methods = array();
+ $this->Crud->initialize($controller);
}
/**
@@ -61,17 +84,60 @@ public function setUp() {
*/
public function tearDown() {
unset($this->Crud);
-
parent::tearDown();
}
public function testDefaultSettings() {
// TODO throw an exception if actions is not defined
}
+ public function testDeleteActionValidId() {
+ $this->Crud->settings['validateId'] = 'notUuid';
+
+ $id = 1;
+// $this->Crud->executeAction('delete', array($id));
+ }
+
+ public function testRedirect() {
+ $this->Crud->controller->expects($this->atLeastOnce())->method('redirect');
+
+ $subject = $this->Crud->testGetSubject();
+ $this->Crud->testRedirect($subject);
+ }
+
public function testvalidateIdIntValid() {
+ $this->Crud->controller->expects($this->never())->method('redirect');
+
+ $this->Crud->settings['validateId'] = 'notUuid';
+
$id = 1;
+ $return = $this->Crud->testValidateId($id, 'int');
+ $this->assertTrue($return, "Expected id $id to be accepted, it was rejected");
+ }
+
+ public function testvalidateIdIntInvalid() {
+ $this->Crud->controller->expects($this->atLeastOnce())->method('redirect');
+
+ $this->Crud->settings['validateId'] = 'notUuid';
+
+ $id = 'abc';
+ $return = $this->Crud->testValidateId($id, 'int');
+ $this->assertFalse($return, "Expected id $id to be rejected, it was accepted");
+ }
+
+ public function testvalidateIdUUIDValid() {
+ $this->Crud->controller->expects($this->never())->method('redirect');
+
+ $id = '12345678-1234-1234-1234-123456789012';
$return = $this->Crud->testValidateId($id);
- $this->assertTrue($return, "Expected $id to be accepted as valid, it was rejected");
+ $this->assertTrue($return, "Expected id $id to be accepted, it was rejected");
+ }
+
+ public function testvalidateIdUUIDInvalid() {
+ $this->Crud->controller->expects($this->atLeastOnce())->method('redirect');
+
+ $id = 123;
+ $return = $this->Crud->testValidateId($id, 'int');
+ $this->assertFalse($return, "Expected id $id to be rejected, it was accepted");
}
}
View
27 Controller/Component/CrudComponent.php
@@ -130,7 +130,7 @@ public function initialize(Controller $controller) {
}
/**
- * The startup method is called after the controller’s beforeFilter method
+ * The startup method is called after the controller’s beforeFilter method
* but before the controller executes the current action handler.
*
* We catch our CRUD actions here before Cake complains about them
@@ -546,16 +546,29 @@ protected function redirect($subject, $url = null) {
*
* @return boolean
*/
- protected function validateId($id) {
- if (empty($this->settings['validateId']) || $this->settings['validateId'] === 'uuid') {
+ protected function validateId($id, $type = null) {
+ if (empty($type)) {
+ if (!empty($this->settings['validateId'])) {
+ $type = $this->settings['validateId'];
+ } else {
+ $type = 'uuid';
+ }
+ }
+ if ($type === 'uuid') {
$valid = Validation::uuid($id);
} else {
$valid = is_numeric($id);
+
}
- if (!$valid) {
- $this->eventManager->dispatch(new CakeEvent('Crud.invalidId', $subject = $this->getSubject(compact('id'))));
- $this->redirect($subject, $this->controller->referer());
+ if ($valid) {
+ return true;
}
+
+ $subject = $this->getSubject(compact('id'));
+ $this->eventManager->dispatch(new CakeEvent('Crud.invalidId', $subject));
+ $this->redirect($subject, $this->controller->referer());
+
+ return false;
}
-}
+}

0 comments on commit 46912c3

Please sign in to comment.
Something went wrong with that request. Please try again.