silverstripe-contentreview/tests/php/Forms/ReviewContentHandlerTest.php

84 lines
3.1 KiB
PHP
Raw Normal View History

<?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;
2021-11-01 02:35:22 +01:00
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()
{
2021-11-01 02:35:22 +01:00
$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 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());
}
}