From fdf9317b293443e2bd38bdfb1783d9e2c2971bcb Mon Sep 17 00:00:00 2001 From: Daniel Hensby Date: Fri, 24 Oct 2014 14:09:08 +0100 Subject: [PATCH] Fixing fragile BlogEntryTest We can control the time in tests using `mock_now`, so this is being used to guarantee the date is populated correctly. --- tests/BlogEntryTest.php | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/tests/BlogEntryTest.php b/tests/BlogEntryTest.php index fe4e2b5..3d9bc94 100644 --- a/tests/BlogEntryTest.php +++ b/tests/BlogEntryTest.php @@ -6,11 +6,15 @@ */ class BlogEntryTest extends SapphireTest { static $fixture_file = 'blog/tests/BlogTest.yml'; - + /** * Tests that the blog entry populate defaults works */ public function testPopulateDefaults() { + + // We cant test by the second, as that will most likely fail + SS_Datetime::set_mock_now(SS_Datetime::now()); + $member = $this->objFromFixture("Member", "blogOwner1"); $member->logIn(); @@ -21,43 +25,42 @@ class BlogEntryTest extends SapphireTest { $entry->URLSegment = "test-post"; $entry->Tags = "tag1,tag2"; - // We cant test by the second, as that will most likely fail - $now = date('Y-m-d', strtotime('now')); - - $this->assertContains($now, $entry->Date); + $this->assertEquals(SS_Datetime::now()->Rfc2822(), $entry->dbObject('Date')->Rfc2822()); $this->assertEquals($member->getName(), $entry->Author); $member->logOut(); + + SS_Datetime::clear_mock_now(); } - + /** * Tests BBCode functionality */ public function testBBCodeContent() { - $tmpFlag = BlogEntry::$allow_wysiwyg_editing; - BlogEntry::$allow_wysiwyg_editing = false; - + $tmpFlag = BlogEntry::$allow_wysiwyg_editing; + BlogEntry::$allow_wysiwyg_editing = false; + $entry = $this->objFromFixture('BlogEntry', 'testpost'); $entry->Content = "[url=admin]the CMS[/url]"; $this->assertEquals('

the CMS

', $entry->Content()->value); - BlogEntry::$allow_wysiwyg_editing = $tmpFlag; + BlogEntry::$allow_wysiwyg_editing = $tmpFlag; } /** * Tests BlogEntry::Content method */ public function testContent() { - $tmpFlag = BlogEntry::$allow_wysiwyg_editing; - BlogEntry::$allow_wysiwyg_editing = true; - + $tmpFlag = BlogEntry::$allow_wysiwyg_editing; + BlogEntry::$allow_wysiwyg_editing = true; + $entry = $this->objFromFixture('BlogEntry', 'testpost'); $entry->Content = 'the CMS'; $this->assertEquals('the CMS', $entry->Content()); - BlogEntry::$allow_wysiwyg_editing = $tmpFlag; + BlogEntry::$allow_wysiwyg_editing = $tmpFlag; } - + /** * Tests TagCollection parsing of tags */ @@ -66,7 +69,7 @@ class BlogEntryTest extends SapphireTest { $entry->Tags = 'damian,Bob, andrew , multiple words, thing,tag,item , Andrew'; $tags = $entry->TagNames(); ksort($tags); - + $this->assertEquals(array( 'andrew' => 'Andrew', 'bob' => 'Bob', @@ -77,5 +80,5 @@ class BlogEntryTest extends SapphireTest { 'thing' => 'thing' ), $tags); } - + }