silverstripe-contentreview/tests/php/Forms/ReviewContentHandlerTest.php
Mo Alsharaf 1cf0f01a35
NEW Allow for optional can permission method for content review (#152)
* Allow for optional can permission method for content review

* Content Review permission logic

With the new `canReviewContent()` permission checker we only need to check the permission is set for the user and `canBeReviewedBy()` will always check if the page object is due for review by its owner.

Thus removed redundant logic in `canUseReviewContent()` and accordingly renamed the class filename for additional context.

Co-authored-by: Jared Dreyer <jared.dreyer@silverstripe.com>
2022-06-22 12:25:59 +12:00

91 lines
3.5 KiB
PHP

<?php
namespace SilverStripe\ContentReview\Tests\Forms;
use Page;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Control\Controller;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\ContentReview\Forms\ReviewContentHandler;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\HiddenField;
use SilverStripe\Forms\TextareaField;
use SilverStripe\Security\Member;
use SilverStripe\ORM\ValidationException;
class ReviewContentHandlerTest extends SapphireTest
{
public function testForm()
{
$page = Page::create();
$page->Title = 'Test';
$page->write();
$form = ReviewContentHandler::create()->Form($page);
$this->assertInstanceOf(Form::class, $form);
$this->assertSame('ReviewContentForm', $form->getName());
$this->assertInstanceOf(HiddenField::class, $form->Fields()->fieldByName('ID'));
$this->assertInstanceOf(HiddenField::class, $form->Fields()->fieldByName('ClassName'));
$this->assertInstanceOf(TextareaField::class, $form->Fields()->fieldByName('Review'));
$saveAction = $form->Actions()->first();
$this->assertNotNull($saveAction);
$this->assertTrue($saveAction->hasClass('review-content__action'));
}
public function testExceptionThrownWhenSubmittingReviewForInvalidObject()
{
$this->expectException(ValidationException::class);
$this->expectExceptionMessage('It seems you don\'t have the necessary permissions to submit a content review');
ReviewContentHandler::create()->submitReview(new Member, ['foo' => 'bar']);
}
public function testExceptionThrownWhenSubmittingReviewForInvalidDataObject()
{
$this->expectException(ValidationException::class);
$this->expectExceptionMessage('It seems you don\'t have the necessary permissions to submit a content review');
ReviewContentHandler::create()->submitReview(new Controller, ['foo' => 'bar']);
}
public function testAddReviewNoteCalledWhenSubmittingReview()
{
$this->logInWithPermission('ADMIN');
$controller = new Controller;
$request = new HTTPRequest('GET', '/');
$controller->setRequest($request);
Injector::inst()->registerservice($request);
$mock = $this->getMockBuilder(ReviewContentHandler::class)
->setConstructorArgs([$controller])
->setMethods(['canSubmitReview'])
->getMock();
$mock->expects($this->exactly(3))->method('canSubmitReview')->willReturn(true);
// Via CMS
$request->addHeader('X-Formschema-Request', true);
$result = $mock->submitReview(new SiteTree, ['Review' => 'testing']);
$this->assertSame('Review successfully added', $result);
$request->removeHeader('X-Formschema-Request');
// Via AJAX
$request->addHeader('X-Requested-With', 'XMLHttpRequest');
$result = $mock->submitReview(new SiteTree, ['Review' => 'testing']);
$this->assertInstanceOf(HTTPResponse::class, $result);
$this->assertSame(200, $result->getStatusCode());
$this->assertSame('Review successfully added', $result->getBody());
$request->removeHeader('X-Requested-With');
// Default
$result = $mock->submitReview(new SiteTree, ['Review' => 'testing']);
$this->assertInstanceOf(HTTPResponse::class, $result);
$this->assertSame(302, $result->getStatusCode());
}
}