From 2ec23039ed03e4d50615b5bde8d50cdc3eb51406 Mon Sep 17 00:00:00 2001 From: Tom Rix Date: Thu, 25 Feb 2010 03:34:39 +0000 Subject: [PATCH] MINOR unit tests for the content review module --- code/ContentReviewEmails.php | 2 +- code/PagesDueForReviewReport.php | 6 +- code/SiteTreeContentReview.php | 2 +- tests/ContentReviewTest.php | 102 +++++++++++++++++++++++++++++++ tests/ContentReviewTest.yml | 48 +++++++++++++++ 5 files changed, 155 insertions(+), 5 deletions(-) create mode 100644 tests/ContentReviewTest.php create mode 100644 tests/ContentReviewTest.yml diff --git a/code/ContentReviewEmails.php b/code/ContentReviewEmails.php index 123c5ba..f9931fe 100644 --- a/code/ContentReviewEmails.php +++ b/code/ContentReviewEmails.php @@ -15,7 +15,7 @@ class ContentReviewEmails extends DailyTask { Subsite::$disable_subsite_filter = true; } - $pages = DataObject::get('Page', "SiteTree.NextReviewDate = '".date('Y-m-d')."' AND SiteTree.OwnerID != 0"); + $pages = DataObject::get('Page', "SiteTree.NextReviewDate = '".(class_exists('SS_Datetime') ? SS_Datetime::now()->URLDate() : SSDatetime::now()->URLDate())."' AND SiteTree.OwnerID != 0"); if ($pages && $pages->Count()) { foreach($pages as $page) { $owner = $page->Owner(); diff --git a/code/PagesDueForReviewReport.php b/code/PagesDueForReviewReport.php index 3bea4a2..6dcb0f7 100644 --- a/code/PagesDueForReviewReport.php +++ b/code/PagesDueForReviewReport.php @@ -92,14 +92,14 @@ class PagesDueForReviewReport extends SS_Report { } else { // Review date before if(!empty($params['ReviewDateBefore'])) { - list($day, $month, $year) = explode('/', $_REQUEST['ReviewDateBefore']); + list($day, $month, $year) = explode('/', $params['ReviewDateBefore']); $reviewDate = "$year-$month-$day"; $wheres[] = 'NextReviewDate < \'' . Convert::raw2sql($reviewDate) . '\' + INTERVAL 1 DAY'; } // Review date after if(!empty($params['ReviewDateAfter'])) { - list($day, $month, $year) = explode('/', $_REQUEST['ReviewDateAfter']); + list($day, $month, $year) = explode('/', $params['ReviewDateAfter']); $reviewDate = "$year-$month-$day"; $wheres[] = 'NextReviewDate >= \'' . Convert::raw2sql($reviewDate) . '\''; } @@ -148,7 +148,7 @@ class PagesDueForReviewReport extends SS_Report { // var_dump($records); if($records) { foreach($records as $record) { - $record->LastEditedByName = $record->LastEditedBy()->Title; + $record->LastEditedByName = $record->LastEditedBy() ? $record->LastEditedBy()->Title : null; } if($sort && $field != "LastEditedByName") $records->sort($sort); diff --git a/code/SiteTreeContentReview.php b/code/SiteTreeContentReview.php index 83ee6f6..981171f 100644 --- a/code/SiteTreeContentReview.php +++ b/code/SiteTreeContentReview.php @@ -21,7 +21,7 @@ class SiteTreeContentReview extends DataObjectDecorator implements PermissionPro } function getOwnerName() { - if($this->owner->Owner()) return $this->owner->Owner()->FirstName . ' ' . $this->owner->Owner()->Surname; + if($this->owner->OwnerID && $this->owner->Owner()) return $this->owner->Owner()->FirstName . ' ' . $this->owner->Owner()->Surname; } public function updateCMSFields(&$fields) { diff --git a/tests/ContentReviewTest.php b/tests/ContentReviewTest.php new file mode 100644 index 0000000..169f968 --- /dev/null +++ b/tests/ContentReviewTest.php @@ -0,0 +1,102 @@ +objFromFixture('Member', 'editor'); + $author = $this->objFromFixture('Member', 'author'); + + // Assert the permission code exists + $perms = singleton('SiteTreeContentReview')->providePermissions(); + $this->assertTrue(isset($perms['EDIT_CONTENT_REVIEW_FIELDS'])); + + // Check a user with permission can edit fields + $this->logInAs($editor); + $page = new Page(); + $fields = $page->getCMSFields(); + $this->assertNotNull($fields->fieldByName('Root.Review')); + + // Check a user without permission can see tab + $this->logInAs($author); + $page = new Page(); + $fields = $page->getCMSFields(); + $this->assertNull($fields->fieldByName('Root.Review')); + } + + function testContentReviewEmails() { + SS_Datetime::set_mock_now('2010-02-14 12:00:00'); + + $task = new ContentReviewEmails(); + $task->run(new SS_HTTPRequest('GET', '/dev/tasks/ContentReviewEmails')); + + $this->assertEmailSent('author@example.com', null, sprintf(_t('ContentReviewEmails.SUBJECT', 'Page %s due for content review'), 'Staff')); + + SS_Datetime::clear_mock_now(); + } + + function testAutomaticallySettingReviewDate() { + $editor = $this->objFromFixture('Member', 'editor'); + $this->logInAs($editor); + + $page = new Page(); + $page->ReviewPeriodDays = 10; + $page->write(); + $this->assertTrue($page->doPublish()); + $this->assertEquals(date('Y-m-d', strtotime('now + 10 days')), $page->NextReviewDate); + } + + function testReportContent() { + $editor = $this->objFromFixture('Member', 'editor'); + $this->logInAs($editor); + $report = new PagesDueForReviewReport(); + + $report->parameterFields(); + $report->columns(); + $report->title(); + + $results = $report->sourceRecords(array( + 'ReviewDateAfter' => '01/01/2010', + 'ReviewDateBefore' => '12/12/2010' + ), 'NextReviewDate ASC', false); + + $this->assertEquals($results->column('Title'), array( + 'Home', + 'About Us', + 'Staff', + 'Contact Us' + )); + + SS_Datetime::set_mock_now('2010-02-13 12:00:00'); + + $results = $report->sourceRecords(array( + ), 'NextReviewDate ASC', false); + + $this->assertEquals($results->column('Title'), array( + 'Home', + 'About Us' + )); + + SS_Datetime::clear_mock_now(); + } + + function testOwnerName() { + $editor = $this->objFromFixture('Member', 'editor'); + $this->logInAs($editor); + + $page = new Page(); + $page->ReviewPeriodDays = 10; + $page->OwnerID = $editor->ID; + $page->write(); + + $this->assertTrue($page->doPublish()); + $this->assertEquals($page->OwnerName, "Test Editor"); + + $page = $this->objFromFixture('Page', 'about'); + $page->OwnerID = 0; + $page->write(); + + $this->assertTrue($page->doPublish()); + $this->assertNull($page->OwnerName); + } +} diff --git a/tests/ContentReviewTest.yml b/tests/ContentReviewTest.yml new file mode 100644 index 0000000..5a6d80c --- /dev/null +++ b/tests/ContentReviewTest.yml @@ -0,0 +1,48 @@ +Permission: + cmsmain1: + Code: CMS_ACCESS_CMSMain + cmsmain2: + Code: CMS_ACCESS_CMSMain + setreviewdates: + Code: EDIT_CONTENT_REVIEW_FIELDS + workflowadmin1: + Code: IS_WORKFLOW_ADMIN + workflowadmin2: + Code: IS_WORKFLOW_ADMIN + +Group: + editorgroup: + Title: Edit existing pages + Code: editorgroup + Permissions: =>Permission.cmsmain1,=>Permission.workflowadmin1,=>Permission.setreviewdates + authorgroup: + Title: Author existing pages + Code: authorgroup + Permissions: =>Permission.cmsmain2,=>Permission.workflowadmin2 + + +Member: + author: + FirstName: Test + Surname: Author + Email: author@example.com + Groups: =>Group.authorgroup + editor: + FirstName: Test + Surname: Editor + Groups: =>Group.editorgroup + +Page: + home: + Title: Home + NextReviewDate: 2010-02-01 + about: + Title: About Us + NextReviewDate: 2010-02-07 + staff: + Title: Staff + NextReviewDate: 2010-02-14 + Owner: =>Member.author + contact: + Title: Contact Us + NextReviewDate: 2010-02-21 \ No newline at end of file