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)); }
public function testShouldDeleteIfAllCheckPointsPassed() { $user = $this->createUserAndLoggedIn(array('Blog.manage' => 1, 'Blog.delete' => 1)); $createdContent = $this->createDummyData($user); // begin $result = $this->commandDispatcher->dispatchFromArray('Darryldecode\\Backend\\Components\\ContentBuilder\\Commands\\DeleteContentCommand', array('id' => $createdContent->id)); $this->assertTrue($result->isSuccessful()); $this->assertEquals(200, $result->getStatusCode()); $this->assertEquals('Blog content successfully deleted.', $result->getMessage()); // there should be no contents now $this->assertCount(0, Content::all()->toArray(), 'There should be no contents now'); }
/** * @param Content $content * @param Dispatcher $dispatcher * @return CommandResult */ public function handle(Content $content, Dispatcher $dispatcher) { // get the content if (!($c = $content->with(array('type', 'metaData'))->find($this->id))) { return new CommandResult(false, "Content not found.", null, 404); } // get content available permissions $cTypeDeletePermission = $c->type->type . '.delete'; // check if user has permission if (!$this->disablePermissionChecking) { if (!$this->user->hasAnyPermission([$cTypeDeletePermission])) { return new CommandResult(false, "Not enough permission.", null, 403); } } // fire deleting event $dispatcher->fire($c->type->type . '.deleting', array($c)); // begin delete $c->metaData()->delete(); $c->delete(); // fire deleted event $dispatcher->fire($c->type->type . '.deleted', array($c)); // all good return new CommandResult(true, $c->type->type . ' content successfully deleted.', null, 200); }
public function testCreateContentShouldBePersistedWhenAllValidationPassed() { $loggedInUser = $this->createUserAndLoggedIn(); $contentType = $this->createContentType(); // dummy request $request = Request::create('', 'GET', array('title' => 'Some Title', 'body' => 'Some Body', 'slug' => 'some-slug', 'status' => 'published', 'authorId' => $loggedInUser->id, 'contentTypeId' => $contentType->id, 'taxonomies' => array(), 'metaData' => array('form_1' => array('meta1' => 'meta value 1', 'meta2' => 'meta value 2')), 'miscData' => array())); // begin $result = $this->commandDispatcher->dispatchFrom(Darryldecode\Backend\Components\ContentBuilder\Commands\CreateContentCommand::class, $request); $this->assertTrue($result->isSuccessful()); $this->assertEquals(201, $result->getStatusCode()); $this->assertEquals('Content successfully created.', $result->getMessage()); // prove it exist $content = Content::with('metaData')->find($result->getData()->id); $this->assertEquals('Some Title', $content->title); $this->assertEquals('Some Body', $content->body); $this->assertEquals('some-slug', $content->slug); //$this->assertEquals('published', $content->status); this passed but annoying for IDE mapper giving error $this->assertEquals($loggedInUser->id, $content->author_id); $this->assertEquals($contentType->id, $content->content_type_id); $contentMeta = Content::parseMetaData($content->metaData->toArray()); $this->assertCount(2, $contentMeta['form_1']); $this->assertArrayHasKey('meta1', $contentMeta['form_1']); $this->assertArrayHasKey('meta2', $contentMeta['form_1']); }
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())); }
/** * @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); }