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.
This commit is contained in:
Daniel Hensby 2014-10-24 14:09:08 +01:00
parent 611865f88f
commit fdf9317b29

View File

@ -6,11 +6,15 @@
*/ */
class BlogEntryTest extends SapphireTest { class BlogEntryTest extends SapphireTest {
static $fixture_file = 'blog/tests/BlogTest.yml'; static $fixture_file = 'blog/tests/BlogTest.yml';
/** /**
* Tests that the blog entry populate defaults works * Tests that the blog entry populate defaults works
*/ */
public function testPopulateDefaults() { 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 = $this->objFromFixture("Member", "blogOwner1");
$member->logIn(); $member->logIn();
@ -21,43 +25,42 @@ class BlogEntryTest extends SapphireTest {
$entry->URLSegment = "test-post"; $entry->URLSegment = "test-post";
$entry->Tags = "tag1,tag2"; $entry->Tags = "tag1,tag2";
// We cant test by the second, as that will most likely fail $this->assertEquals(SS_Datetime::now()->Rfc2822(), $entry->dbObject('Date')->Rfc2822());
$now = date('Y-m-d', strtotime('now'));
$this->assertContains($now, $entry->Date);
$this->assertEquals($member->getName(), $entry->Author); $this->assertEquals($member->getName(), $entry->Author);
$member->logOut(); $member->logOut();
SS_Datetime::clear_mock_now();
} }
/** /**
* Tests BBCode functionality * Tests BBCode functionality
*/ */
public function testBBCodeContent() { public function testBBCodeContent() {
$tmpFlag = BlogEntry::$allow_wysiwyg_editing; $tmpFlag = BlogEntry::$allow_wysiwyg_editing;
BlogEntry::$allow_wysiwyg_editing = false; BlogEntry::$allow_wysiwyg_editing = false;
$entry = $this->objFromFixture('BlogEntry', 'testpost'); $entry = $this->objFromFixture('BlogEntry', 'testpost');
$entry->Content = "[url=admin]the CMS[/url]"; $entry->Content = "[url=admin]the CMS[/url]";
$this->assertEquals('<p><a href="admin">the CMS</a></p>', $entry->Content()->value); $this->assertEquals('<p><a href="admin">the CMS</a></p>', $entry->Content()->value);
BlogEntry::$allow_wysiwyg_editing = $tmpFlag; BlogEntry::$allow_wysiwyg_editing = $tmpFlag;
} }
/** /**
* Tests BlogEntry::Content method * Tests BlogEntry::Content method
*/ */
public function testContent() { public function testContent() {
$tmpFlag = BlogEntry::$allow_wysiwyg_editing; $tmpFlag = BlogEntry::$allow_wysiwyg_editing;
BlogEntry::$allow_wysiwyg_editing = true; BlogEntry::$allow_wysiwyg_editing = true;
$entry = $this->objFromFixture('BlogEntry', 'testpost'); $entry = $this->objFromFixture('BlogEntry', 'testpost');
$entry->Content = '<a href="admin">the CMS</a>'; $entry->Content = '<a href="admin">the CMS</a>';
$this->assertEquals('<a href="admin">the CMS</a>', $entry->Content()); $this->assertEquals('<a href="admin">the CMS</a>', $entry->Content());
BlogEntry::$allow_wysiwyg_editing = $tmpFlag; BlogEntry::$allow_wysiwyg_editing = $tmpFlag;
} }
/** /**
* Tests TagCollection parsing of tags * Tests TagCollection parsing of tags
*/ */
@ -66,7 +69,7 @@ class BlogEntryTest extends SapphireTest {
$entry->Tags = 'damian,Bob, andrew , multiple words, thing,tag,item , Andrew'; $entry->Tags = 'damian,Bob, andrew , multiple words, thing,tag,item , Andrew';
$tags = $entry->TagNames(); $tags = $entry->TagNames();
ksort($tags); ksort($tags);
$this->assertEquals(array( $this->assertEquals(array(
'andrew' => 'Andrew', 'andrew' => 'Andrew',
'bob' => 'Bob', 'bob' => 'Bob',
@ -77,5 +80,5 @@ class BlogEntryTest extends SapphireTest {
'thing' => 'thing' 'thing' => 'thing'
), $tags); ), $tags);
} }
} }