1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\ContentReview\Tests; |
4
|
|
|
|
5
|
|
|
use Page; |
6
|
|
|
use SilverStripe\CMS\Controllers\CMSPageEditController; |
7
|
|
|
use SilverStripe\CMS\Model\SiteTree; |
8
|
|
|
use SilverStripe\ContentReview\Extensions\SiteTreeContentReview; |
9
|
|
|
use SilverStripe\ContentReview\Extensions\ContentReviewOwner; |
10
|
|
|
use SilverStripe\ContentReview\Extensions\ContentReviewCMSExtension; |
11
|
|
|
use SilverStripe\ContentReview\Extensions\ContentReviewDefaultSettings; |
12
|
|
|
use SilverStripe\Control\HTTPRequest; |
13
|
|
|
use SilverStripe\Control\HTTPResponse_Exception; |
14
|
|
|
use SilverStripe\Forms\FieldList; |
15
|
|
|
use SilverStripe\Forms\Form; |
16
|
|
|
use SilverStripe\Security\Group; |
17
|
|
|
use SilverStripe\Security\Member; |
18
|
|
|
use SilverStripe\SiteConfig\SiteConfig; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @mixin PHPUnit_Framework_TestCase |
22
|
|
|
*/ |
23
|
|
|
class ContentReviewCMSPageEditControllerTest extends ContentReviewBaseTest |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected static $fixture_file = 'ContentReviewTest.yml'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
protected static $required_extensions = [ |
34
|
|
|
SiteTree::class => [SiteTreeContentReview::class], |
35
|
|
|
Group::class => [ContentReviewOwner::class], |
36
|
|
|
Member::class => [ContentReviewOwner::class], |
37
|
|
|
CMSPageEditController::class => [ContentReviewCMSExtension::class], |
38
|
|
|
SiteConfig::class => [ContentReviewDefaultSettings::class], |
39
|
|
|
]; |
40
|
|
|
|
41
|
|
View Code Duplication |
public function testReviewedThrowsExceptionWithNoRecordID() |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
$this->setExpectedException(HTTPResponse_Exception::class); |
|
|
|
|
44
|
|
|
|
45
|
|
|
/** @var CMSPageEditController|ContentReviewCMSExtension $controller */ |
46
|
|
|
$controller = new CMSPageEditController(); |
47
|
|
|
|
48
|
|
|
$dummyForm = new Form($controller, "EditForm", new FieldList(), new FieldList()); |
49
|
|
|
|
50
|
|
|
$controller->savereview(array( |
51
|
|
|
"ID" => null, |
52
|
|
|
"Message" => null, |
53
|
|
|
), $dummyForm); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
View Code Duplication |
public function testReviewedThrowsExceptionWithWrongRecordID() |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
$this->setExpectedException(HTTPResponse_Exception::class); |
|
|
|
|
59
|
|
|
|
60
|
|
|
/** @var CMSPageEditController|ContentReviewCMSExtension $controller */ |
61
|
|
|
$controller = new CMSPageEditController(); |
62
|
|
|
|
63
|
|
|
$dummyForm = new Form($controller, "EditForm", new FieldList(), new FieldList()); |
64
|
|
|
|
65
|
|
|
$controller->savereview(array( |
66
|
|
|
"ID" => "FAIL", |
67
|
|
|
"Message" => null, |
68
|
|
|
), $dummyForm); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testReviewedWithAuthor() |
72
|
|
|
{ |
73
|
|
|
/** @var Member $author */ |
74
|
|
|
$author = $this->objFromFixture(Member::class, "author"); |
75
|
|
|
|
76
|
|
|
$this->logInAs($author); |
77
|
|
|
|
78
|
|
|
/** @var Page|SiteTreeContentReview $page */ |
79
|
|
|
$page = $this->objFromFixture(Page::class, "home"); |
80
|
|
|
|
81
|
|
|
$data = array( |
82
|
|
|
"action_savereview" => 1, |
83
|
|
|
"ID" => $page->ID, |
84
|
|
|
); |
85
|
|
|
|
86
|
|
|
$this->get('admin/pages/edit/show/' . $page->ID); |
87
|
|
|
$response = $this->post($this->getFormAction($page), $data); |
88
|
|
|
|
89
|
|
|
$this->assertEquals("OK", $response->getStatusDescription()); |
90
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function testSaveReview() |
94
|
|
|
{ |
95
|
|
|
/** @var Member $author */ |
96
|
|
|
$author = $this->objFromFixture(Member::class, "author"); |
97
|
|
|
|
98
|
|
|
$this->logInAs($author); |
99
|
|
|
|
100
|
|
|
/** @var Page|SiteTreeContentReview $page */ |
101
|
|
|
$page = $this->objFromFixture(Page::class, "home"); |
102
|
|
|
|
103
|
|
|
$data = array( |
104
|
|
|
"action_savereview" => 1, |
105
|
|
|
"ID" => $page->ID, |
106
|
|
|
"ReviewNotes" => "This is the best page ever", |
107
|
|
|
); |
108
|
|
|
|
109
|
|
|
$this->get('admin/pages/edit/show/' . $page->ID); |
110
|
|
|
$response = $this->post($this->getFormAction($page), $data); |
111
|
|
|
|
112
|
|
|
$this->assertEquals("OK", $response->getStatusDescription()); |
113
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
114
|
|
|
$this->assertEquals(1, $page->ReviewLogs()->count()); |
115
|
|
|
|
116
|
|
|
$reviewLog = $page->ReviewLogs()->first(); |
117
|
|
|
|
118
|
|
|
$this->assertEquals($data["ReviewNotes"], $reviewLog->Note); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Return a CMS page edit form action via using a dummy request and session |
123
|
|
|
* |
124
|
|
|
* @param Page $page |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
|
|
protected function getFormAction(Page $page) |
128
|
|
|
{ |
129
|
|
|
$controller = singleton(CMSPageEditController::class); |
130
|
|
|
$controller->setRequest(new HTTPRequest('GET', '/')); |
131
|
|
|
$controller->getRequest()->setSession($this->session()); |
132
|
|
|
|
133
|
|
|
return $controller->getEditForm($page->ID)->FormAction(); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.