/** * @param ContentTypeTaxonomy $contentTypeTaxonomy * @param Dispatcher $dispatcher * @return CommandResult */ public function handle(ContentTypeTaxonomy $contentTypeTaxonomy, Dispatcher $dispatcher) { // fire before query $dispatcher->fire('taxonomyTerms.beforeQuery', array($this->args)); // begin if (!($taxonomy = $contentTypeTaxonomy->with('terms')->find($this->taxonomyId))) { return new CommandResult(false, "Taxonomy not found.", null, 404); } // fire after query $dispatcher->fire('taxonomyTerms.afterQuery', array($this->args)); // all good return new CommandResult(true, "Query terms by taxonomy command successful.", $taxonomy->terms, 200); }
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)); }
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)); }
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; }
/** * 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 ContentTypeTaxonomy $contentTypeTaxonomy * @param Dispatcher $dispatcher * @return CommandResult */ public function handle(ContentTypeTaxonomy $contentTypeTaxonomy, 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); } } // fire creating event $dispatcher->fire('taxonomy.deleting', array($this->args)); if (!($taxonomy = $contentTypeTaxonomy->with(array('terms', 'terms.contents'))->find($this->taxonomyId))) { return new CommandResult(false, "Taxonomy not found.", null, 404); } // detach all contents that are related to its terms $taxonomy->terms->each(function ($term) { $term->contents()->detach(); }); // delete taxonomy $taxonomy->delete(); // fire creating event $dispatcher->fire('taxonomy.deleted', array($taxonomy)); // all good return new CommandResult(true, "Taxonomy successfully deleted.", null, 200); }
/** * */ public function testContentTypeShouldBeCreatedWhenAllCheckPointsPassed() { // 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); $contentType = $this->createContentType(); // required taxonomy field $request = Request::create('', 'GET', array('taxonomy' => 'categories', 'description' => 'some description', 'contentTypeId' => $contentType->id)); $result = $this->commandDispatcher->dispatchFromArray('Darryldecode\\Backend\\Components\\ContentBuilder\\Commands\\CreateContentTypeTaxonomyCommand', array('taxonomy' => $request->get('taxonomy', null), 'description' => $request->get('description', null), 'contentTypeId' => $request->get('contentTypeId', null))); $this->assertTrue($result->isSuccessful()); $this->assertEquals(201, $result->getStatusCode()); $this->assertEquals('Content type taxonomy successfully created.', $result->getMessage()); // verify content type taxonomy $cTypeTaxonomy = ContentTypeTaxonomy::find($result->getData()->id); $this->assertEquals($result->getData()->id, $cTypeTaxonomy->id); $this->assertEquals('categories', $cTypeTaxonomy->taxonomy); }
public function testInvalidContentType() { // 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 taxonomy for blog content type $taxonomy = ContentTypeTaxonomy::create(array('content_type_id' => 2, 'taxonomy' => 'categories', 'description' => '')); // term field required $request = Request::create('', 'GET', array('term' => '', 'contentTypeTaxonomyId' => $taxonomy->id)); $result = $this->commandDispatcher->dispatchFrom('Darryldecode\\Backend\\Components\\ContentBuilder\\Commands\\CreateTypeTaxonomyTerm', $request); $this->assertFalse($result->isSuccessful()); $this->assertEquals(400, $result->getStatusCode()); $this->assertEquals('Invalid Content Type.', $result->getMessage()); }