/** * Test the creation of a blog article based off the newly created blog * * @depends testCreateBlog */ public function testCreateBlogArticle() { // Update the current user so it has admin access. \Core\user()->set('admin', true); // Setup some variables that will be used throughout this method. $title = 'New Test Blog Article'; $randomsnippet = 'Random-Snippet-' . Core::RandomHex(10); $lorem = new BaconIpsumGenerator(); $body = $lorem->getParagraph(1); // Tack on the random snipped I'll be looking for. $body .= $lorem->getParagraphsAsMarkup(8, $randomsnippet); $blog = new BlogModel(self::$TestBlogID); $request = new PageRequest('/blog/article/create/' . self::$TestBlogID); $request->execute(); $view = $request->getView(); $this->assertEquals(200, $view->error, 'Checking that article creation returns a valid page'); // The returned data should have a "form" available. This is the actual creation form. /** @var $form Form */ $form = $view->getVariable('form'); $this->assertInstanceOf('Form', $form, 'Checking that the form is set from the blog article create controller'); // Set some variables on the form $form->getElement('page[title]')->set('value', $title); $form->getElement('page[rewriteurl]')->set('value', $blog->get('rewriteurl') . '/' . \Core\str_to_url($title)); $form->getElement('model[image]')->set('value', 'public/blog/blog-test-image.png'); $form->getElement('model[body]')->set('value', $body); // Copy in the image $src = \Core\Filestore\Factory::File(ROOT_PDIR . 'components/blog/tests/blog-test-image.png'); /** @var $dest \Core\Filestore\File */ $dest = \Core\Filestore\Factory::File('public/blog/blog-test-image.png'); $src->copyTo($dest, true); // make sure that it exists $this->assertTrue($dest->exists(), 'Checking that files can be copied into the public filestore'); // And submit this form to the handler. // On a successful submission, it should be simply the URL of the blog. $formsubmission = call_user_func_array($form->get('callsmethod'), array($form)); if ($formsubmission === false) { throw new Exception(implode("\n", $form->getErrors())); } // Go to the parent listing page and find this entry. $request = new PageRequest($blog->get('rewriteurl')); $request->execute(); $view = $request->getView(); $this->assertEquals(200, $view->error); $html = $view->fetch(); $this->assertContains($title, $html); $this->assertContains('itemtype="http://schema.org/BlogPosting"', $html); preg_match_all('#<div[^>]*itemtype="http://schema.org/BlogPosting"[^>]*>.*<a[^>]*href="(.*)"[^>]*>(.*)</a>#msU', $html, $matches); // Title should now have three keys, with at least one value each. $this->assertNotEmpty($matches[1]); $this->assertNotEmpty($matches[2]); // This node contains the URL. $foundurl = $matches[1][0]; $foundtitle = trim($matches[2][0]); // Make sure the url contains the site url. $this->assertStringStartsWith(ROOT_URL, $foundurl); // And trim it off. This is because PageRequest expects that the url is already trimmed. $foundurl = '/' . substr($foundurl, strlen(ROOT_URL)); $this->assertEquals($title, $foundtitle); //$this->assertStringStartsWith('/blog/article/view/', $formsubmission, 'Checking that blog article creation was successful'); // Go to the page and make sure that it loads up! $request = new PageRequest($foundurl); $request->execute(); $view = $request->getView(); $this->assertEquals(200, $view->error, 'Checking that public blog article exists'); $html = $view->fetch(); $this->assertContains($title, $html, 'Checking that the public blog article page contains the correct title'); $this->assertContains($randomsnippet, $html, 'Checking that the public blog article page contains the correct body'); $this->assertContains('blog-test-image', $html, 'Checking that the public blog article page contains the correct image'); }