/** * @param Factory $validator * @param Dispatcher $dispatcher * @param ContentType $contentType * @param ContentTypeFormGroup $contentTypeFormGroup * @return CommandResult */ public function handle(Factory $validator, Dispatcher $dispatcher, ContentType $contentType, ContentTypeFormGroup $contentTypeFormGroup) { // check if user has permission if (!$this->disablePermissionChecking) { if (!$this->user->hasAnyPermission(['contentBuilder.manage'])) { return new CommandResult(false, "Not enough permission.", null, 403); } } // validate data if (!is_null($this->fields) && count($this->fields) == 0) { return new CommandResult(false, "Fields should have atleast 1 item", null, 400); } // fire event creating $dispatcher->fire('formGroup.updating', array($this->args)); // begin create if (!($formGroup = $contentTypeFormGroup->find($this->id))) { return new CommandResult(false, "Form group Not Found.", null, 400); } $formGroup->name = $this->name ? $this->name : $formGroup->name; $formGroup->form_name = $this->formName ? $this->formName : $formGroup->form_name; $formGroup->conditions = $this->conditions ? $this->conditions : $formGroup->conditions; $formGroup->fields = $this->fields ? $this->fields : $formGroup->fields; $formGroup->content_type_id = $this->contentTypeId ? $this->contentTypeId : $formGroup->content_type_id; if (!$formGroup->save()) { return new CommandResult(false, "Failed to update form group.", null, 400); } // fire event creating $dispatcher->fire('formGroup.updated', array($formGroup)); // all good return new CommandResult(true, "Form group successfully updated.", $formGroup, 200); }
/** * @param ContentTypeFormGroup $contentTypeFormGroup * @param Dispatcher $dispatcher * @return CommandResult */ public function handle(ContentTypeFormGroup $contentTypeFormGroup, Dispatcher $dispatcher) { // check if user has permission if (!$this->disablePermissionChecking) { if (!$this->user->hasAnyPermission(['contentBuilder.delete'])) { return new CommandResult(false, "Not enough permission.", null, 403); } } if (!($formGroup = $contentTypeFormGroup->find($this->id))) { return new CommandResult(false, "Form Group Not Found.", null, 404); } // fire before delete event $dispatcher->fire('formGroup.deleting', array($formGroup)); $formGroup->delete(); // fire after delete event $dispatcher->fire('formGroup.deleted', array($formGroup)); // all good return new CommandResult(true, "Form group successfully deleted.", null, 200); }
public function testDeletingAndDeletedEvent() { $user = $this->createUserAndLoggedIn(); // create event dispatcher mock and inject it to laravel application $eventDispatcherMock = $this->getMockBuilder('Illuminate\\Events\\Dispatcher')->setMethods(array('fire'))->getMock(); // se expectations $eventDispatcherMock->expects($this->exactly(2))->method('fire')->withConsecutive(array($this->equalTo('formGroup.deleting'), $this->isType('array')), array($this->equalTo('formGroup.deleted'), $this->isType('array'))); // inject to laravel "events" IoC alias $this->application['events'] = $eventDispatcherMock; // create dummy form group $fGroup = ContentTypeFormGroup::create(array('name' => 'Event Organizer Details', 'form_name' => 'event_organizer_details', 'conditions' => array(), 'fields' => array(), 'content_type_id' => 1)); // begin $this->commandDispatcher->dispatchFromArray('Darryldecode\\Backend\\Components\\ContentBuilder\\Commands\\DeleteFormGroupCommand', array('id' => $fGroup->id)); }
public function testShouldDeleteFormGroup() { $user = User::create(array('first_name' => 'darryl', 'email' => '*****@*****.**', 'password' => 'pass$darryl', 'permissions' => array('superuser' => 1))); $this->application['auth']->loginUsingId($user->id); // create dummy form group $fGroup = ContentTypeFormGroup::create(array('name' => 'Event Organizer Details', 'form_name' => 'event_organizer_details', 'conditions' => array(), 'fields' => array(), 'content_type_id' => 1)); // begin $result = $this->commandDispatcher->dispatchFromArray('Darryldecode\\Backend\\Components\\ContentBuilder\\Commands\\DeleteFormGroupCommand', array('id' => $fGroup->id)); $this->assertTrue($result->isSuccessful(), "transaction should success."); $this->assertEquals(200, $result->getStatusCode(), "status code should be ok"); $this->assertEquals('Form group successfully deleted.', $result->getMessage()); // prove the form group do not exist $this->assertInternalType('null', ContentTypeFormGroup::find($fGroup->id)); }
public function testUpdatingAndUpdatedEvent() { $user = $this->createUserAndLoggedIn(); // create event dispatcher mock and inject it to laravel application $eventDispatcherMock = $this->getMockBuilder('Illuminate\\Events\\Dispatcher')->setMethods(array('fire'))->getMock(); // se expectations $eventDispatcherMock->expects($this->exactly(2))->method('fire')->withConsecutive(array($this->equalTo('formGroup.updating'), $this->isType('array')), array($this->equalTo('formGroup.updated'), $this->isType('array'))); // inject to laravel "events" IoC alias $this->application['events'] = $eventDispatcherMock; // create a dummy Form group $formGroup = ContentTypeFormGroup::create(array('name' => 'Event Location', 'form_name' => 'event_location', 'conditions' => array(), 'fields' => array(), 'content_type_id' => 2)); // begin $result = $this->commandDispatcher->dispatchFromArray('Darryldecode\\Backend\\Components\\ContentBuilder\\Commands\\UpdateFormGroupCommand', array('id' => $formGroup->id)); $this->assertTrue($result->isSuccessful(), "transaction should be successful."); $this->assertEquals(200, $result->getStatusCode(), "status code should be 201"); $this->assertEquals('Form group successfully updated.', $result->getMessage()); }
public function testBeforeAndAfterQueryEvents() { // create user and logged in (the user who will perform the action) $user = User::create(array('first_name' => 'dianne', 'email' => '*****@*****.**', 'password' => 'pass$dianne', 'permissions' => array('superuser' => 1))); // logged in the user $this->application['auth']->loginUsingId($user->id); // create event dispatcher mock and inject it to laravel application $eventDispatcherMock = $this->getMockBuilder('Illuminate\\Events\\Dispatcher')->setMethods(array('fire'))->getMock(); // se expectations $eventDispatcherMock->expects($this->exactly(2))->method('fire')->withConsecutive(array($this->equalTo('formGroup.beforeQuery'), $this->isType('array')), array($this->equalTo('formGroup.afterQuery'), $this->isType('array'))); // inject to laravel "events" IoC alias $this->application['events'] = $eventDispatcherMock; // create a dummy content type $contentType = ContentType::create(array('type' => 'Event', 'enable_revisions' => true)); // create dummy Form Groups ContentTypeFormGroup::create(array('name' => 'Event Organizer', 'form_name' => 'event_organizer', 'conditions' => array(), 'fields' => array(), 'content_type_id' => $contentType->id)); // begin $result = $this->commandDispatcher->dispatchFromArray('Darryldecode\\Backend\\Components\\ContentBuilder\\Commands\\QueryFormGroupCommand', array('paginated' => true, 'perPage' => 6, 'contentTypeId' => $contentType->id)); }
public function testShouldUpdateWithoutFillingAllFields() { $user = User::create(array('first_name' => 'darryl', 'email' => '*****@*****.**', 'password' => 'pass$darryl', 'permissions' => array('superuser' => 1))); $this->application['auth']->loginUsingId($user->id); // create a dummy Form group $formGroup = ContentTypeFormGroup::create(array('name' => 'Event Location', 'form_name' => 'event_location', 'conditions' => array(), 'fields' => array(), 'content_type_id' => 2)); // begin $result = $this->commandDispatcher->dispatchFromArray('Darryldecode\\Backend\\Components\\ContentBuilder\\Commands\\UpdateFormGroupCommand', array('id' => $formGroup->id)); $this->assertTrue($result->isSuccessful(), "transaction should be successful."); $this->assertEquals(200, $result->getStatusCode(), "status code should be ok"); $this->assertEquals('Form group successfully updated.', $result->getMessage()); // fields should have the same value as there is no provided values $fGroup = ContentTypeFormGroup::find($formGroup->id); //$this->assertEquals('Event Location', $fGroup->name); $this->assertEquals('event_location', $fGroup->form_name); $this->assertCount(0, $fGroup->conditions); $this->assertCount(0, $fGroup->fields); $this->assertEquals(2, $fGroup->content_type_id); }
public function testQueryShouldReturnCollectionObjectIfQuestedWithNotPaginated() { $user = User::create(array('first_name' => 'darryl', 'email' => '*****@*****.**', 'password' => 'pass$darryl', 'permissions' => array('Event.manage' => 1))); $this->application['auth']->loginUsingId($user->id); // create a dummy content type $contentType = ContentType::create(array('type' => 'Event', 'enable_revisions' => true)); // create dummy Form Groups ContentTypeFormGroup::create(array('name' => 'Event Organizer', 'form_name' => 'event_organizer', 'conditions' => array(), 'fields' => array(), 'content_type_id' => $contentType->id)); ContentTypeFormGroup::create(array('name' => 'Event Location', 'form_name' => 'event_location', 'conditions' => array(), 'fields' => array(), 'content_type_id' => $contentType->id)); // begin $result = $this->commandDispatcher->dispatchFromArray('Darryldecode\\Backend\\Components\\ContentBuilder\\Commands\\QueryFormGroupCommand', array('paginated' => false, 'perPage' => 6, 'contentTypeId' => $contentType->id)); $this->assertTrue($result->isSuccessful(), "transaction should be successful."); $this->assertEquals(200, $result->getStatusCode(), "status code should be ok"); $this->assertEquals('Query form groups command successful.', $result->getMessage()); // prove paginated instance $this->assertInstanceOf('Illuminate\\Database\\Eloquent\\Collection', $result->getData()); // prove has two items $this->assertCount(2, $result->getData()->toArray()); }
/** * @param ContentTypeFormGroup $contentTypeFormGroup * @param Dispatcher $dispatcher * @return CommandResult */ protected function queryAll($contentTypeFormGroup, $dispatcher) { if (!$this->disablePermissionChecking) { if (!$this->user->isSuperUser()) { return new CommandResult(false, CommandResult::$responseForbiddenMessage, null, 403); } } // fire before query $dispatcher->fire('formGroup.beforeQuery', array($this->args)); if ($this->paginated) { $res = $contentTypeFormGroup->with(array('contentType'))->paginate($this->perPage); } else { $res = $contentTypeFormGroup->with(array('contentType'))->get(); } // fire after query $dispatcher->fire('formGroup.afterQuery', array($res)); // all good return new CommandResult(true, "Query form groups command successful.", $res, 200); }