/** * @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 $validationResult = $validator->make(array('name' => $this->name, 'form_name' => $this->formName, 'fields' => $this->fields, 'content_type_id' => $this->contentTypeId), ContentTypeFormGroup::$rules); if ($validationResult->fails()) { return new CommandResult(false, $validationResult->getMessageBag()->first(), null, 400); } // fire event creating $dispatcher->fire('formGroup.creating', array($this->args)); // begin create if (!($cType = $contentType->find($this->contentTypeId))) { return new CommandResult(false, "Content Type Not Found.", null, 400); } $createdFormGroup = $cType->formGroups()->create(array('name' => $this->name, 'form_name' => $this->formName, 'conditions' => $this->conditions, 'fields' => $this->fields, 'content_type_id' => $this->contentTypeId)); // fire event creating $dispatcher->fire('formGroup.created', array($createdFormGroup)); // all good return new CommandResult(true, "Form group successfully created.", $createdFormGroup, 201); }
/** * @param ContentType $contentType * @param Dispatcher $dispatcher * @return CommandResult */ public function handle(ContentType $contentType, Dispatcher $dispatcher) { // begin before query $dispatcher->fire('contentType.beforeQuery', array($this->args)); // check if has permission if (!$this->disablePermissionChecking) { // if $type->type is not provided, the request referrer should be from // the admin UI Content Type Builder component. // so we will check if the user has permission (contentBuilder.manage) // on the other hand, // if $type->type is provided, we will check if user has permission to manage that type if (!is_null($this->type) && $this->type != '') { if (!$this->user->hasAnyPermission([$this->type . '.manage'])) { return new CommandResult(false, "Not enough permission.", null, 403); } } else { if (!$this->user->hasAnyPermission(['contentBuilder.manage'])) { return new CommandResult(false, "Not enough permission.", null, 403); } } } // begin query $results = $contentType->with(array('terms.taxonomy', 'taxonomies', 'taxonomies.terms', 'formGroups'))->ofType($this->type)->get(); // begin after query $dispatcher->fire('contentType.afterQuery', array($this->args)); // all good return new CommandResult(true, "Query content types successful.", $results, 200); }
/** * @param ContentType $contentType * @param ContentTypeFormGroup $contentTypeFormGroup * @param Dispatcher $dispatcher * @return CommandResult */ protected function queryByContentType($contentType, $contentTypeFormGroup, $dispatcher) { if (!($cType = $contentType->find($this->contentTypeId))) { return new CommandResult(false, 'Content Type not found.', null, 400); } // build needed permissions // if the user has no permissions to manage or create under this Content Type, // the query should be forbidden $cTypeManage = $cType->type . '.manage'; if (!$this->disablePermissionChecking) { if (!$this->user->hasAnyPermission([$cTypeManage, 'contentBuilder.manage'])) { 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'))->where('content_type_id', $this->contentTypeId)->paginate($this->perPage); } else { $res = $contentTypeFormGroup->with(array('contentType'))->where('content_type_id', $this->contentTypeId)->get(); } // fire after query $dispatcher->fire('formGroup.afterQuery', array($res)); // all good return new CommandResult(true, "Query form groups command successful.", $res, 200); }
public function testDeletingAndDeletedEvent() { $loggedInUser = $this->createUserAndLoggedIn(); // create event dispatcher mock and inject it to laravel application $eventDispatcherMock = $this->getMockBuilder('Illuminate\\Events\\Dispatcher')->setMethods(array('fire'))->getMock(); // set expectations $eventDispatcherMock->expects($this->exactly(2))->method('fire')->withConsecutive(array($this->equalTo('taxonomy.deleting'), $this->isType('array')), array($this->equalTo('taxonomy.deleted'), $this->isType('array'))); // inject to laravel "events" IoC alias $this->application['events'] = $eventDispatcherMock; // create dummy content type $cType = ContentType::create(array('type' => 'Blog', 'enable_revisions' => true)); // create taxonomy $taxonomy = ContentTypeTaxonomy::create(array('taxonomy' => 'categories', 'description' => 'some description', 'parent' => null, 'content_type_id' => $cType->id)); // create now terms $lifestyleTerm = ContentTypeTaxonomyTerm::create(array('term' => 'lifestyle', 'slug' => 'lifestyle', 'content_type_taxonomy_id' => $taxonomy->id)); $sportsTerm = ContentTypeTaxonomyTerm::create(array('term' => 'sports', 'slug' => 'sports', 'content_type_taxonomy_id' => $taxonomy->id)); $codingTerm = ContentTypeTaxonomyTerm::create(array('term' => 'coding', 'slug' => 'coding', 'content_type_taxonomy_id' => $taxonomy->id)); // create a Blog post $content = Content::create(array('title' => 'Blog Post Title', 'body' => 'Some blog body', 'slug' => 'blog-post-title', 'status' => Content::CONTENT_PUBLISHED, 'author_id' => $loggedInUser->id, 'content_type_id' => $cType->id)); $content->terms()->attach($lifestyleTerm); $content->terms()->attach($sportsTerm); $content->terms()->attach($codingTerm); // begin $this->commandDispatcher->dispatchFromArray('Darryldecode\\Backend\\Components\\ContentBuilder\\Commands\\DeleteTaxonomyCommand', array('taxonomyId' => $taxonomy->id)); }
/** * the component navigation * * @return ComponentNavigationCollection */ public function getNavigation() { // the content builder navigation $contentBuilder = new ComponentNavigation('Content Builder', 'fa fa-th-large', url(config('backend.backend.base_url') . '/content_types')); $contentBuilder->setRequiredPermissions(['contentBuilder.manage']); // the form group:form builder:custom fields $formBuilder = new ComponentNavigation('Custom Fields', 'fa fa-list', url(config('backend.backend.base_url') . '/custom_fields')); $formBuilder->setRequiredPermissions(['contentBuilder.manage']); // the contents navigation $contentsNavigation = new ComponentNavigation('Contents', 'fa fa-book', '', true); // when "php artisan" command is run, all application registered services are run, // so during "php artisan package:publish" stuffs this will throw error as ContentType::all() // has no migrations yet. Let's try catch here so we can solve that problem try { foreach (ContentType::all() as $type) { $n = new ComponentNavigation($type->type, 'fa fa-pencil-square', url(config('backend.backend.base_url') . '/contents/' . $type->type)); // the permission requirements to check $permissionManage = $type->type . '.manage'; $n->setRequiredPermissions([$permissionManage]); $contentsNavigation->addSubMenu($n); } } catch (\Exception $e) { // there is an error, do nothing } $navs = new ComponentNavigationCollection(); $navs->push($contentBuilder); $navs->push($formBuilder); $navs->push($contentsNavigation); return $navs; }
public function testShouldDeleteTaxonomy() { // this is not a super user $user = User::create(array('first_name' => 'darryl', 'email' => '*****@*****.**', 'password' => 'pass$darryl', 'permissions' => array('superuser' => 1))); $this->application['auth']->loginUsingId($user->id); // create dummy content type $cType = ContentType::create(array('type' => 'Blog', 'enable_revisions' => true)); // create taxonomy and give it also a child $taxonomy = ContentTypeTaxonomy::create(array('taxonomy' => 'categories', 'description' => 'some description', 'content_type_id' => $cType->id)); $childTaxonomy = ContentTypeTaxonomy::create(array('taxonomy' => 'categories-child', 'description' => 'some description', 'content_type_id' => $cType->id)); // create now terms $lifestyleTerm = ContentTypeTaxonomyTerm::create(array('term' => 'lifestyle', 'slug' => 'lifestyle', 'content_type_taxonomy_id' => $taxonomy->id)); $sportsTerm = ContentTypeTaxonomyTerm::create(array('term' => 'sports', 'slug' => 'sports', 'content_type_taxonomy_id' => $taxonomy->id)); $codingTerm = ContentTypeTaxonomyTerm::create(array('term' => 'coding', 'slug' => 'coding', 'content_type_taxonomy_id' => $taxonomy->id)); // create a Blog post $content = Content::create(array('title' => 'Blog Post Title', 'body' => 'Some blog body', 'slug' => 'blog-post-title', 'status' => Content::CONTENT_PUBLISHED, 'author_id' => $user->id, 'content_type_id' => $cType->id)); $content->terms()->attach($lifestyleTerm); $content->terms()->attach($sportsTerm); $content->terms()->attach($codingTerm); // lets verify first that the post was indeed to have those 3 terms $c = Content::with('terms')->find($content->id); $this->assertCount(3, $c->terms->toArray(), "The Blog post should have 3 terms"); $this->assertCount(3, ContentTypeTaxonomyTerm::all()->toArray(), "Ther should be 3 terms"); // now lets start to delete the taxonomy // it should also delete the terms and the BLog post should no longer have those terms $result = $this->commandDispatcher->dispatchFromArray('Darryldecode\\Backend\\Components\\ContentBuilder\\Commands\\DeleteTaxonomyCommand', array('taxonomyId' => $taxonomy->id)); $this->assertTrue($result->isSuccessful()); $this->assertEquals(200, $result->getStatusCode()); $this->assertEquals('Taxonomy successfully deleted.', $result->getMessage()); // the blog post should not contain any terms anymore $c = Content::with('terms')->find($content->id); $this->assertCount(0, $c->terms->toArray(), "The Blog Post should not have any terms anymore"); // taxonomy should be delete $this->assertInternalType('null', ContentTypeTaxonomy::find($taxonomy->id)); }
protected function createDummyData() { // create content type $blogContentType = ContentType::create(array('type' => 'blog', 'enable_revisions' => true)); // create taxonomy for blog content type $taxonomy = $blogContentType->taxonomies()->create(array('taxonomy' => 'categories', 'description' => '')); // create the terms $taxonomy->terms()->create(array('term' => 'lifestyle', 'slug' => 'lifestyle')); }
protected function createDummyData() { $cType = ContentType::create(array('type' => 'Blog', 'enable_revisions' => true)); $taxonomy = ContentTypeTaxonomy::create(array('taxonomy' => 'categories', 'description' => 'some description', 'content_type_id' => $cType->id)); // create now terms ContentTypeTaxonomyTerm::create(array('term' => 'lifestyle', 'slug' => 'lifestyle', 'content_type_taxonomy_id' => $taxonomy->id)); ContentTypeTaxonomyTerm::create(array('term' => 'sports', 'slug' => 'sports', 'content_type_taxonomy_id' => $taxonomy->id)); ContentTypeTaxonomyTerm::create(array('term' => 'coding', 'slug' => 'coding', 'content_type_taxonomy_id' => $taxonomy->id)); return $taxonomy; }
public function testShouldCreateFormGroupIfAllCheckPointPassed() { $user = User::create(array('first_name' => 'darryl', 'email' => '*****@*****.**', 'password' => 'pass$darryl', 'permissions' => array('superuser' => 1))); $this->application['auth']->loginUsingId($user->id); // create a dummy content type $contentType = ContentType::create(array('type' => 'Event', 'enable_revisions' => true)); // should require name field $result = $this->commandDispatcher->dispatchFromArray('Darryldecode\\Backend\\Components\\ContentBuilder\\Commands\\CreateFormGroupCommand', array('name' => 'Event Organizer', 'formName' => 'event_organizer', 'conditions' => array(), 'fields' => array('some_field' => 'some'), 'contentTypeId' => $contentType->id)); $this->assertTrue($result->isSuccessful(), "transaction should be successful."); $this->assertEquals(201, $result->getStatusCode(), "status code should be 201"); $this->assertEquals('Form group successfully created.', $result->getMessage()); }
/** * Execute the command. * * @param ContentType $contentType * @param ContentTypeTaxonomy $contentTypeTaxonomy * @param Validator $validator * @param Dispatcher $dispatcher * @return CommandResult */ public function handle(ContentType $contentType, ContentTypeTaxonomy $contentTypeTaxonomy, Validator $validator, Dispatcher $dispatcher) { // in order to determine what permissions are needed to create // a taxonomy terms, we will get first what taxonomy the term is for // after we can get the taxonomy, we will then get what type the taxonomy // belong so we can verify if the user has permission for that type try { $taxonomy = $contentTypeTaxonomy->findOrFail($this->contentTypeTaxonomyId); } catch (\Exception $e) { return new CommandResult(false, "Invalid Taxonomy.", null, 400); } try { $type = $contentType->findOrFail($taxonomy->content_type_id); } catch (\Exception $e) { return new CommandResult(false, "Invalid Content Type.", null, 400); } // build the permissions needed $canManageOnThisType = $type->type . '.manage'; // check if user has permission if (!$this->disablePermissionChecking) { if (!$this->user->hasAnyPermission([$canManageOnThisType])) { return new CommandResult(false, "Not enough permission.", null, 403); } } // validate data $validationResult = $validator->make(array('term' => $this->term, 'slug' => $this->slug, 'content_type_taxonomy_id' => $this->contentTypeTaxonomyId), ContentTypeTaxonomyTerm::$rules); if ($validationResult->fails()) { return new CommandResult(false, $validationResult->getMessageBag()->first(), null, 400); } // prepare term to be created $termToBeCreated = array('term' => $this->term, 'slug' => $this->slug, 'content_type_taxonomy_id' => $this->contentTypeTaxonomyId); // fire creating event $dispatcher->fire('contentTypeTaxonomyTerm.creating', array($termToBeCreated)); // store $createdTerm = $taxonomy->terms()->create($termToBeCreated); // fire creating event $dispatcher->fire('contentTypeTaxonomyTerm.created', array($createdTerm)); // all good return new CommandResult(true, "Taxonomy term successfully created.", $createdTerm, 201); }
/** * @param ContentType $contentType * @param ContentTypeTaxonomy $contentTypeTaxonomy * @param ContentTypeTaxonomyTerm $contentTypeTaxonomyTerm * @param Dispatcher $dispatcher * @return CommandResult */ public function handle(ContentType $contentType, ContentTypeTaxonomy $contentTypeTaxonomy, ContentTypeTaxonomyTerm $contentTypeTaxonomyTerm, Dispatcher $dispatcher) { // in order to determine what permissions are needed to create // a taxonomy terms, we will get first what taxonomy the term is for // after we can get the taxonomy, we will then get what type the taxonomy // belong so we can verify if the user has permission for that type try { $taxonomy = $contentTypeTaxonomy->findOrFail($this->taxonomyId); } catch (\Exception $e) { return new CommandResult(false, "Invalid Taxonomy.", null, 400); } try { $type = $contentType->findOrFail($taxonomy->content_type_id); } catch (\Exception $e) { return new CommandResult(false, "Invalid Content Type.", null, 400); } try { $term = $contentTypeTaxonomyTerm->findOrFail($this->termId); } catch (\Exception $e) { return new CommandResult(false, "Invalid Taxonomy Term.", null, 400); } // build the permissions needed $canManageOnThisType = $type->type . '.manage'; // check if user has permission if (!$this->disablePermissionChecking) { if (!$this->user->hasAnyPermission([$canManageOnThisType])) { return new CommandResult(false, "Not enough permission.", null, 403); } } // fire creating event $dispatcher->fire('taxonomyTerm.deleting', array($this->args)); // detach all posts type related to this term first $term->contents()->detach(); $term->delete(); // fire creating event $dispatcher->fire('taxonomyTerm.deleted', array($taxonomy)); // all good return new CommandResult(true, "Taxonomy Term successfully deleted.", null, 200); }
/** * @param Dispatcher $dispatcher * @param ContentType $contentType * @return CommandResult */ public function handle(Dispatcher $dispatcher, ContentType $contentType) { // check if user has permission if (!$this->disablePermissionChecking) { if (!$this->user->hasAnyPermission(['contentBuilder.delete'])) { return new CommandResult(false, "Not enough permission.", null, 403); } } if (!($cType = $contentType->find($this->contentTypeId))) { return new CommandResult(false, "Content type not found.", null, 400); } // prevent deletion if content Type as contents if ($this->contentTypeHasContents($cType)) { return new CommandResult(false, "Content type has contents. Delete Contents first.", null, 400); } // fire before delete $dispatcher->fire('contentType.deleting', array($this->args)); $cType->delete(); // fire before delete $dispatcher->fire('contentType.deleted', array($cType)); // all good return new CommandResult(true, "Content type successfully deleted.", null, 200); }
/** * Execute the command. * * @param Factory $validator * @param ContentType $contentType * @param Dispatcher $dispatcher * @return CommandResult */ public function handle(Factory $validator, ContentType $contentType, Dispatcher $dispatcher) { // validate authorization if (!$this->disablePermissionChecking) { if (!$this->user->hasAnyPermission(['contentBuilder.manage'])) { return new CommandResult(false, CommandResult::$responseForbiddenMessage, null, 403); } } // validate data $validationResult = $validator->make(array('type' => $this->type), ContentType::$rules); if ($validationResult->fails()) { return new CommandResult(false, $validationResult->getMessageBag()->first(), null, 400); } // prepare data to be created $contentTypeToBeCreated = array('type' => $this->type, 'enable_revisions' => $this->enableRevision == 'no' ? false : true); // fire content type creating event $dispatcher->fire('contentType.creating', array($contentTypeToBeCreated)); // store $createdContentType = $contentType->create($contentTypeToBeCreated); // fire content type created event $dispatcher->fire('contentType.created', array($createdContentType)); // return return new CommandResult(true, "Content type successfully created.", $createdContentType, 201); }
public function testShouldDeleteContentType() { // 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 dummy content type $blog = ContentType::create(array('type' => 'Blog', 'enable_revisions' => true)); // begin $result = $this->commandDispatcher->dispatchFromArray('Darryldecode\\Backend\\Components\\ContentBuilder\\Commands\\DeleteContentTypeCommand', array('contentTypeId' => $blog->id)); $this->assertTrue($result->isSuccessful()); $this->assertEquals('Content type successfully deleted.', $result->getMessage()); $this->assertEquals(200, $result->getStatusCode(), 'Status code should be ok'); // prove content type has been deleted $this->assertInternalType('null', ContentType::find($blog->id)); }
public function testDeletingAndDeletedEventShouldBeFiredWhenContentTypeIsSuccessfullyDeleted() { // 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('contentType.deleting'), $this->isType('array')), array($this->equalTo('contentType.deleted'), $this->isType('array'))); // inject to laravel "events" IoC alias $this->application['events'] = $eventDispatcherMock; // create dummy content type $blog = ContentType::create(array('type' => 'Blog', 'enable_revisions' => true)); // begin $this->commandDispatcher->dispatchFromArray('Darryldecode\\Backend\\Components\\ContentBuilder\\Commands\\DeleteContentTypeCommand', array('contentTypeId' => $blog->id)); }
public function testCreatingAndCreatedEvent() { $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.creating'), $this->isType('array')), array($this->equalTo('formGroup.created'), $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)); // should require name field $result = $this->commandDispatcher->dispatchFromArray('Darryldecode\\Backend\\Components\\ContentBuilder\\Commands\\CreateFormGroupCommand', array('name' => 'Event Organizer', 'formName' => 'event_organizer', 'conditions' => array(), 'fields' => array('some_field' => 'some'), 'contentTypeId' => $contentType->id)); $this->assertTrue($result->isSuccessful(), "transaction should be successful."); $this->assertEquals(201, $result->getStatusCode(), "status code should be 201"); $this->assertEquals('Form group successfully created.', $result->getMessage()); }
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()); }
public function testCreatingAndCreatedEvent() { // create user and logged in $user = User::create(array('first_name' => 'darryl', 'email' => '*****@*****.**', 'password' => 'pass$darryl', 'permissions' => array('superuser' => 1))); $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('contentTypeTaxonomyTerm.creating'), $this->isType('array')), array($this->equalTo('contentTypeTaxonomyTerm.created'), $this->isType('array'))); // inject to laravel "events" IoC alias $this->application['events'] = $eventDispatcherMock; // create content type $blogContentType = ContentType::create(array('type' => 'blog', 'enable_revisions' => true)); // create taxonomy for blog content type $taxonomy = $blogContentType->taxonomies()->create(array('taxonomy' => 'categories', 'description' => '', 'parent' => null)); // term field required $request = Request::create('', 'GET', array('term' => 'lifestyle', 'slug' => 'lifestyle', 'parent' => '', 'contentTypeTaxonomyId' => $taxonomy->id)); $this->commandDispatcher->dispatchFrom('Darryldecode\\Backend\\Components\\ContentBuilder\\Commands\\CreateTypeTaxonomyTerm', $request); }
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)); }
protected function createDummyData($user) { // create content type $ctype = ContentType::create(array('type' => 'Blog', 'enable_revisions' => true)); // create content type taxonomy $taxonomy = $this->commandDispatcher->dispatchFromArray('Darryldecode\\Backend\\Components\\ContentBuilder\\Commands\\CreateContentTypeTaxonomyCommand', array('taxonomy' => 'category', 'description' => 'the blog post category', 'contentTypeId' => $ctype->id)); $taxonomy = $taxonomy->getData()->toArray(); // create taxonomy terms $technologyTerm = $this->commandDispatcher->dispatchFromArray('Darryldecode\\Backend\\Components\\ContentBuilder\\Commands\\CreateTypeTaxonomyTerm', array('term' => 'technology', 'slug' => 'technology', 'contentTypeTaxonomyId' => $taxonomy['id'])); $technologyTerm = $technologyTerm->getData()->toArray(); $healthTerm = $this->commandDispatcher->dispatchFromArray('Darryldecode\\Backend\\Components\\ContentBuilder\\Commands\\CreateTypeTaxonomyTerm', array('term' => 'health', 'slug' => 'health', 'contentTypeTaxonomyId' => $taxonomy['id'])); $healthTerm = $healthTerm->getData()->toArray(); $programmingTerm = $this->commandDispatcher->dispatchFromArray('Darryldecode\\Backend\\Components\\ContentBuilder\\Commands\\CreateTypeTaxonomyTerm', array('term' => 'programming', 'slug' => 'programming', 'contentTypeTaxonomyId' => $taxonomy['id'])); $programmingTerm = $programmingTerm->getData()->toArray(); // create blog post dummy contents // this will have ID of 1, we will use this on test query by ID $this->commandDispatcher->dispatchFromArray('Darryldecode\\Backend\\Components\\ContentBuilder\\Commands\\CreateContentCommand', array('title' => 'Some Title', 'body' => 'Some Body', 'slug' => 'some-title', 'status' => 'published', 'authorId' => $user->id, 'contentTypeId' => $ctype->id, 'taxonomies' => array($healthTerm['id'] => true), 'metaData' => array('form_1' => array('meta1' => 'meta value 1', 'meta2' => 'meta value 2')), 'miscData' => array())); $this->commandDispatcher->dispatchFromArray('Darryldecode\\Backend\\Components\\ContentBuilder\\Commands\\CreateContentCommand', array('title' => 'Some Entry 2', 'body' => 'Some entry entry', 'slug' => 'some-entry-2', 'status' => 'published', 'authorId' => $user->id, 'contentTypeId' => $ctype->id, 'taxonomies' => array($programmingTerm['id'] => true, $healthTerm['id'] => true), 'metaData' => array('form_1' => array('meta1' => 'meta value 1', 'meta2' => 'meta value 2')), 'miscData' => array())); $this->commandDispatcher->dispatchFromArray('Darryldecode\\Backend\\Components\\ContentBuilder\\Commands\\CreateContentCommand', array('title' => 'Some Entry 3', 'body' => 'Some entry entry 3', 'slug' => 'some-entry-3', 'status' => 'published', 'authorId' => $user->id, 'contentTypeId' => $ctype->id, 'taxonomies' => array(), 'metaData' => array('form_1' => array('meta1' => 'meta value 1', 'meta2' => 'meta value 2')), 'miscData' => array())); }
protected function createContentType() { return ContentType::create(array('type' => 'blog', 'enable_revisions' => true)); }
/** * Query By content type * * @param ContentType $contentType * @param Content $content * @param $config * @return mixed */ protected function query($contentType, $content, $config) { // prepare content model used $content = $this->createContentModel($content, $config); $q = $content->with(array_merge(array('terms', 'author', 'metaData', 'type.formGroups', 'revisions', 'type'), $this->with)); // check if there is status provided if ($this->status && $this->status != 'any') { $q->where('status', $this->status); } // check if author ID is provided if ($this->authorId) { $q->where('author_id', $this->authorId); } // check if type is provided, we need to provide content type // we will not allow to query all of contents if (!is_null($this->type) && $this->type != '') { if (is_numeric($this->type)) { $cType = $contentType->find($this->type); } else { $cType = $contentType->with(array())->where('type', $this->type)->first(); } if ($cType) { $q->whereHas('type', function ($q) use($cType) { $q->where('type', $cType->type); }); // let's check first if the user querying has the permission to access this kind of content if (!$this->disablePermissionChecking) { $requiredPermission = $cType->type . '.manage'; if (!$this->user->hasAnyPermission([$requiredPermission])) { return new CommandResult(false, "Not enough permission.", null, 403); } } } } else { return new CommandResult(false, "Content Type should be provided.", null, 400); } // check if terms are provided so we can include it in query conditions if (!is_null($this->terms) && $this->terms != '') { $tax = $this->extractTerms($this->terms); if (count($tax) > 0) { foreach ($tax as $k => $v) { $q->whereHas('terms', function ($q) use($k, $v) { $q->whereHas('taxonomy', function ($q) use($k) { $q->where('taxonomy', $k); }); if (is_string($v)) { $q->where('slug', $v); } else { $q->whereIn('slug', $v); } }); } } } // setup date ranges if (!is_null($this->startDate) && $this->startDate != '') { $q->ofStartDate($this->startDate); } if (!is_null($this->endDate) && $this->endDate != '') { $q->ofEndDate($this->endDate); } // trigger query hook if provided if (!is_null($this->queryHook) && is_callable($this->queryHook)) { if ($res = call_user_func($this->queryHook, $q)) { $q = $res; } } // sort order $q->orderBy($this->sortBy, $this->sortOrder); // decide whether request wants paginated version or not if ($this->paginated) { $res = $q->paginate($this->perPage); } else { $res = $q->get(); } return $res; }
/** * @param Content $content * @param ContentType $contentType * @param Dispatcher $dispatcher * @return CommandResult */ public function handle(Content $content, ContentType $contentType, Dispatcher $dispatcher) { // get content available permissions try { $cType = $contentType->findOrFail($this->contentTypeId); $cTypeManage = $cType->type . '.manage'; } catch (\Exception $e) { return new CommandResult(false, "Invalid Content Type.", null, 400); } // check if user has permission if (!$this->disablePermissionChecking) { if (!$this->user->hasAnyPermission([$cTypeManage])) { return new CommandResult(false, "Not enough permission.", null, 403); } } if (!($c = $content->find($this->id))) { return new CommandResult(false, "Content not found.", null, 404); } // fire event updating $dispatcher->fire($cType->type . '.updating', array($c, $this->args)); // hold the current content so we can use it later if revisions is enabled $oldBody = $c->body; $c->title = $this->title ? $this->title : $c->title; $c->body = $this->body ? $this->body : $c->body; $c->slug = $this->slug ? $this->slug : $c->slug; $c->status = $this->status ? $this->status : $c->status; $c->permission_requirements = $this->permissionRequirements ? $this->permissionRequirements : $c->permission_requirements; $c->misc_data = $this->miscData ? $this->miscData : $c->misc_data; // taxonomy if ($this->taxonomies) { // detach all taxonomies first $c->terms()->detach(); foreach ($this->taxonomies as $termId => $value) { if ($value == true) { $c->terms()->attach(array('content_type_taxonomy_term_id' => $termId)); } } } // meta data if ($this->metaData) { // clear all meta data first $c->metaData()->delete(); foreach ($this->metaData as $formGroup => $formGroupData) { foreach ($formGroupData as $metaKey => $metaValue) { $c->metaData()->create(array('key' => $metaKey, 'value' => $metaValue, 'form_group_name' => $formGroup)); } } } // save $c->save(); // check if revisions is enabled so we can deal with it if ($cType->enable_revisions == ContentType::REVISIONS_ENABLED) { if ($oldBody != $c->body) { $c->revisions()->create(array('old_content' => $oldBody, 'new_content' => $c->body, 'author_id' => $this->user->id)); } } // fire event updated $dispatcher->fire($cType->type . '.updated', array($c)); // return response return new CommandResult(true, "Content successfully updated.", $c, 200); }
public function testShouldDeleteTerm() { // create user and logged in $user = User::create(array('first_name' => 'darryl', 'email' => '*****@*****.**', 'password' => 'pass$darryl', 'permissions' => array('blog.manage' => 1))); $this->application['auth']->loginUsingId($user->id); // create content type $blogContentType = ContentType::create(array('type' => 'blog', 'enable_revisions' => true)); // create taxonomy for blog content type $taxonomy = $blogContentType->taxonomies()->create(array('taxonomy' => 'categories', 'description' => '')); // add term to the taxonomy $taxonomy->terms()->create(array('term' => 'coding', 'slug' => 'coding')); // let's prove first we have now 1 taxonomy term for categories which is coding $this->assertCount(1, ContentTypeTaxonomyTerm::all()->toArray()); $this->assertEquals('coding', ContentTypeTaxonomyTerm::all()->first()->term); // now let's delete the term $request = Request::create('', 'GET', array('taxonomyId' => $blogContentType->id, 'termId' => $taxonomy->id)); $result = $this->commandDispatcher->dispatchFrom('Darryldecode\\Backend\\Components\\ContentBuilder\\Commands\\DeleteTaxonomyTermCommand', $request); // we should not be able to delete it because we don't have a permission for a blog to manage $this->assertTrue($result->isSuccessful()); $this->assertEquals('Taxonomy Term successfully deleted.', $result->getMessage()); $this->assertEquals(200, $result->getStatusCode()); // let's prove that there is no term now on our records $this->assertEmpty(ContentTypeTaxonomyTerm::all()->toArray()); }
protected function seedSampleContentTypes() { ContentType::create(array('type' => 'blog', 'enable_revisions' => true)); }
protected function createDummyData($user, $enableRevisions = true) { $blogContentType = ContentType::create(array('type' => 'Blog', 'enable_revisions' => $enableRevisions)); return Content::create(array('title' => 'Some Title', 'body' => 'Some Body', 'slug' => 'some-slug', 'status' => 'published', 'author_id' => $user->id, 'content_type_id' => $blogContentType->id, 'taxonomies' => array('luzon'), 'misc_data' => array())); }
public function testTaxonomyShouldBeCreatedWhenAllCheckPointsPassed() { // create user and logged in $user = User::create(array('first_name' => 'darryl', 'email' => '*****@*****.**', 'password' => 'pass$darryl', 'permissions' => array('superuser' => 1))); $this->application['auth']->loginUsingId($user->id); // create content type $blogContentType = ContentType::create(array('type' => 'blog', 'enable_revisions' => true)); // create taxonomy for blog content type $taxonomy = $blogContentType->taxonomies()->create(array('taxonomy' => 'categories', 'description' => '')); // term field required $request = Request::create('', 'GET', array('term' => 'lifestyle', 'slug' => 'lifestyle', 'contentTypeTaxonomyId' => $taxonomy->id)); $result = $this->commandDispatcher->dispatchFrom('Darryldecode\\Backend\\Components\\ContentBuilder\\Commands\\CreateTypeTaxonomyTerm', $request); $this->assertTrue($result->isSuccessful()); $this->assertEquals(201, $result->getStatusCode()); $this->assertEquals('Taxonomy term successfully created.', $result->getMessage()); // verify that term now exist on DB $term = ContentTypeTaxonomyTerm::find($result->getData()->id); $this->assertEquals($result->getData()->id, $term->id); $this->assertEquals('lifestyle', $term->term); $this->assertEquals($taxonomy->id, $term->content_type_taxonomy_id); }
/** * Execute the command. * * @param Content $content * @param Factory $validator * @param ContentType $contentType * @param Dispatcher $dispatcher * @param Repository $config * @return CommandResult */ public function handle(Content $content, Factory $validator, ContentType $contentType, Dispatcher $dispatcher, Repository $config) { $content = $this->createContentModel($content, $config); // get content available permissions try { $cType = $contentType->findOrFail($this->contentTypeId); $cTypeManage = $cType->type . '.manage'; } catch (\Exception $e) { return new CommandResult(false, "Invalid Content Type.", null, 400); } // check if user has permission if (!$this->disablePermissionChecking) { if (!$this->user->hasAnyPermission([$cTypeManage])) { return new CommandResult(false, "Not enough permission.", null, 403); } } // validate data $validationResult = $validator->make(array('title' => $this->title, 'body' => $this->body, 'slug' => $this->slug, 'author_id' => $this->authorId, 'content_type_id' => $this->contentTypeId), $content::$rules); if ($validationResult->fails()) { return new CommandResult(false, $validationResult->getMessageBag()->first(), null, 400); } // prepare data to be store $contentToBeCreated = array('title' => $this->title, 'body' => $this->body, 'slug' => $this->slug, 'status' => Helpers::issetAndHasValueOrAssignDefault($this->status, Content::CONTENT_PUBLISHED), 'author_id' => $this->authorId, 'content_type_id' => $this->contentTypeId, 'meta' => $this->metaData, 'misc_data' => $this->miscData, 'taxonomies' => $this->taxonomies); // fire event creating $dispatcher->fire($cType->type . '.creating', array($contentToBeCreated)); $createdContent = $content->create($contentToBeCreated); // taxonomy foreach ($contentToBeCreated['taxonomies'] as $termId => $value) { if ($value == true) { $createdContent->terms()->attach(array('content_type_taxonomy_term_id' => $termId)); } } // meta foreach ($contentToBeCreated['meta'] as $formGroup => $formGroupData) { foreach ($formGroupData as $metaKey => $metaValue) { $createdContent->metaData()->create(array('key' => $metaKey, 'value' => $metaValue, 'form_group_name' => $formGroup)); } } // fire event created $dispatcher->fire($cType->type . '.created', array($createdContent)); // return response return new CommandResult(true, "Content successfully created.", $createdContent, 201); }
protected function createDummyContentTypes() { ContentType::create(array('type' => 'Blog', 'enable_revisions' => true)); ContentType::create(array('type' => 'Events', 'enable_revisions' => true)); ContentType::create(array('type' => 'News', 'enable_revisions' => true)); }
public function testCreateContentTypeWithDisabledRevisions() { // this is a super user $user = User::create(array('first_name' => 'darryl', 'email' => '*****@*****.**', 'password' => 'pass$darryl', 'permissions' => array('superuser' => 1))); $this->application['auth']->loginUsingId($user->id); $result = $this->commandDispatcher->dispatchFromArray('Darryldecode\\Backend\\Components\\ContentBuilder\\Commands\\CreateContentTypeCommand', array('type' => 'Products', 'enableRevision' => 'no')); $this->assertTrue($result->isSuccessful()); $this->assertEquals(201, $result->getStatusCode()); $this->assertEquals('Content type successfully created.', $result->getMessage()); // prove $cType = ContentType::find($result->getData()->id); $this->assertFalse((bool) $cType->enable_revisions); }