API Stop using deprecated API

This commit is contained in:
Steve Boyd 2022-11-29 09:35:55 +13:00
parent 9bfef9e931
commit b771bb624a
10 changed files with 35 additions and 19 deletions

View File

@ -19,6 +19,7 @@ use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\FieldType\DBHTMLText; use SilverStripe\ORM\FieldType\DBHTMLText;
use SilverStripe\ORM\PaginatedList; use SilverStripe\ORM\PaginatedList;
use SilverStripe\Security\Security; use SilverStripe\Security\Security;
use SilverStripe\Control\Middleware\HTTPCacheControlMiddleware;
/** /**
* @package comments * @package comments
@ -499,7 +500,7 @@ class CommentingController extends Controller
public function redirectBack() public function redirectBack()
{ {
// Don't cache the redirect back ever // Don't cache the redirect back ever
HTTP::set_cache_age(0); HTTPCacheControlMiddleware::singleton()->setMaxAge(0);
$url = null; $url = null;

View File

@ -77,9 +77,9 @@ class CommentForm extends Form
// limited HTML is allowed. Populated by JS/Ajax. // limited HTML is allowed. Populated by JS/Ajax.
if ($usePreview) { if ($usePreview) {
$fields->insertAfter( $fields->insertAfter(
'Comment',
ReadonlyField::create('PreviewComment', _t('CommentInterface.PREVIEWLABEL', 'Preview')) ReadonlyField::create('PreviewComment', _t('CommentInterface.PREVIEWLABEL', 'Preview'))
->setAttribute('style', 'display: none'), // enable through JS ->setAttribute('style', 'display: none') // enable through JS
'Comment'
); );
} }

View File

@ -28,6 +28,7 @@ use SilverStripe\ORM\PaginatedList;
use SilverStripe\ORM\SS_List; use SilverStripe\ORM\SS_List;
use SilverStripe\Security\Member; use SilverStripe\Security\Member;
use SilverStripe\Security\Permission; use SilverStripe\Security\Permission;
use SilverStripe\Security\Security;
/** /**
* Represents a single comment object. * Represents a single comment object.
@ -265,7 +266,12 @@ class Comment extends DataObject
*/ */
public function getParent() public function getParent()
{ {
// this is wrapped in withNoReplacement() because it's called by ViewableData::__get()
// which looks for a `"get$property"` method, which itself is called by
// AssetControllExtension::findAssets()
Deprecation::withNoReplacement(function () {
Deprecation::notice('4.0.0', 'Use $this->Parent() instead'); Deprecation::notice('4.0.0', 'Use $this->Parent() instead');
});
return $this->BaseClass && $this->ParentID return $this->BaseClass && $this->ParentID
? DataObject::get_by_id($this->BaseClass, $this->ParentID, true) ? DataObject::get_by_id($this->BaseClass, $this->ParentID, true)
: null; : null;
@ -428,7 +434,7 @@ class Comment extends DataObject
protected function getMember($member = null) protected function getMember($member = null)
{ {
if (!$member) { if (!$member) {
$member = Member::currentUser(); $member = Security::getCurrentUser();
} }
if (is_numeric($member)) { if (is_numeric($member)) {
@ -477,7 +483,7 @@ class Comment extends DataObject
protected function actionLink($action, $member = null) protected function actionLink($action, $member = null)
{ {
if (!$member) { if (!$member) {
$member = Member::currentUser(); $member = Security::getCurrentUser();
} }
if (!$member) { if (!$member) {
return false; return false;
@ -650,9 +656,9 @@ class Comment extends DataObject
// Show member name if given // Show member name if given
if (($author = $this->Author()) && $author->exists()) { if (($author = $this->Author()) && $author->exists()) {
$fields->insertAfter( $fields->insertAfter(
'Name',
TextField::create('AuthorMember', $this->fieldLabel('Author'), $author->Title) TextField::create('AuthorMember', $this->fieldLabel('Author'), $author->Title)
->performReadonlyTransformation(), ->performReadonlyTransformation()
'Name'
); );
} }

View File

@ -5,6 +5,7 @@ namespace SilverStripe\Comments\Model\Comment;
use SilverStripe\Control\Controller; use SilverStripe\Control\Controller;
use SilverStripe\Security\Member; use SilverStripe\Security\Member;
use SilverStripe\Security\RandomGenerator; use SilverStripe\Security\RandomGenerator;
use SilverStripe\Security\Security;
/** /**
* Provides the ability to generate cryptographically secure tokens for comment moderation * Provides the ability to generate cryptographically secure tokens for comment moderation
@ -87,7 +88,7 @@ class SecurityToken
*/ */
public function checkRequest($request) public function checkRequest($request)
{ {
$member = Member::currentUser(); $member = Security::getCurrentUser();
if (!$member) { if (!$member) {
return false; return false;
} }

View File

@ -25,7 +25,7 @@
<% if $PagedComments %> <% if $PagedComments %>
<ul class="comments-list root-level"> <ul class="comments-list root-level">
<% loop $PagedComments %> <% loop $PagedComments %>
<li class="comment $EvenOdd<% if FirstLast %> $FirstLast <% end_if %> $SpamClass"> <li class="comment $EvenOdd<% if $FirstLast %> $FirstLast <% end_if %> $SpamClass">
<% include CommentsInterface_singlecomment %> <% include CommentsInterface_singlecomment %>
</li> </li>
<% end_loop %> <% end_loop %>

View File

@ -11,7 +11,7 @@
<% if $Replies %> <% if $Replies %>
<ul class="comments-list level-{$Depth}"> <ul class="comments-list level-{$Depth}">
<% loop $Replies %> <% loop $Replies %>
<li class="comment $EvenOdd<% if FirstLast %> $FirstLast <% end_if %> $SpamClass"> <li class="comment $EvenOdd<% if $FirstLast %> $FirstLast <% end_if %> $SpamClass">
<% include CommentsInterface_singlecomment %> <% include CommentsInterface_singlecomment %>
</li> </li>
<% end_loop %> <% end_loop %>

View File

@ -14,6 +14,7 @@ use SilverStripe\Dev\FunctionalTest;
use SilverStripe\ORM\DataObject; use SilverStripe\ORM\DataObject;
use SilverStripe\Security\Member; use SilverStripe\Security\Member;
use SilverStripe\Security\SecurityToken; use SilverStripe\Security\SecurityToken;
use SilverStripe\Security\Security;
class CommentingControllerTest extends FunctionalTest class CommentingControllerTest extends FunctionalTest
{ {
@ -98,7 +99,7 @@ class CommentingControllerTest extends FunctionalTest
$comment = $this->objFromFixture(Comment::class, 'testModeratedComment1'); $comment = $this->objFromFixture(Comment::class, 'testModeratedComment1');
$st = new CommentSecurityToken($comment); $st = new CommentSecurityToken($comment);
$url = 'comments/approve/' . $comment->ID; $url = 'comments/approve/' . $comment->ID;
$url = $st->addToUrl($url, Member::currentUser()); $url = $st->addToUrl($url, Security::getCurrentUser());
$response = $this->get($url, null, ['Referer' => '/']); $response = $this->get($url, null, ['Referer' => '/']);
$this->assertEquals(302, $response->getStatusCode()); $this->assertEquals(302, $response->getStatusCode());
$comment = DataObject::get_by_id(Comment::class, $comment->ID); $comment = DataObject::get_by_id(Comment::class, $comment->ID);
@ -131,7 +132,7 @@ class CommentingControllerTest extends FunctionalTest
$comment->markSpam(); $comment->markSpam();
$st = new CommentSecurityToken($comment); $st = new CommentSecurityToken($comment);
$url = 'comments/ham/' . $comment->ID; $url = 'comments/ham/' . $comment->ID;
$url = $st->addToUrl($url, Member::currentUser()); $url = $st->addToUrl($url, Security::getCurrentUser());
$response = $this->get($url); $response = $this->get($url);
$this->assertEquals(302, $response->getStatusCode()); $this->assertEquals(302, $response->getStatusCode());
$comment = DataObject::get_by_id(Comment::class, $comment->ID); $comment = DataObject::get_by_id(Comment::class, $comment->ID);
@ -153,7 +154,7 @@ class CommentingControllerTest extends FunctionalTest
$comment->markApproved(); $comment->markApproved();
$st = new CommentSecurityToken($comment); $st = new CommentSecurityToken($comment);
$url = 'comments/spam/' . $comment->ID; $url = 'comments/spam/' . $comment->ID;
$url = $st->addToUrl($url, Member::currentUser()); $url = $st->addToUrl($url, Security::getCurrentUser());
$response = $this->get($url); $response = $this->get($url);
$this->assertEquals(302, $response->getStatusCode()); $this->assertEquals(302, $response->getStatusCode());
$comment = DataObject::get_by_id(Comment::class, $comment->ID); $comment = DataObject::get_by_id(Comment::class, $comment->ID);
@ -274,7 +275,7 @@ class CommentingControllerTest extends FunctionalTest
); );
$this->assertEquals(302, $response->getStatusCode()); $this->assertEquals(302, $response->getStatusCode());
// $this->assertStringStartsWith('CommentableItemController#comment-', $response->getHeader('Location')); // $this->assertStringStartsWith('CommentableItemController#comment-', $response->getHeader('Location'));
$this->assertDOSEquals( $this->assertListEquals(
array( array(
array( array(
'Name' => 'Poster', 'Name' => 'Poster',
@ -305,7 +306,7 @@ class CommentingControllerTest extends FunctionalTest
); );
$this->assertEquals(302, $response->getStatusCode()); $this->assertEquals(302, $response->getStatusCode());
// $this->assertStringStartsWith('CommentableItemController#comment-', $response->getHeader('Location')); // $this->assertStringStartsWith('CommentableItemController#comment-', $response->getHeader('Location'));
$this->assertDOSEquals( $this->assertListEquals(
array( array(
array( array(
'Name' => 'Test Author', 'Name' => 'Test Author',

View File

@ -13,6 +13,7 @@ use SilverStripe\Dev\FunctionalTest;
use SilverStripe\Dev\SapphireTest; use SilverStripe\Dev\SapphireTest;
use SilverStripe\Security\Member; use SilverStripe\Security\Member;
use SilverStripe\View\Requirements; use SilverStripe\View\Requirements;
use SilverStripe\Security\Security;
class CommentsExtensionTest extends FunctionalTest class CommentsExtensionTest extends FunctionalTest
{ {
@ -218,8 +219,8 @@ class CommentsExtensionTest extends FunctionalTest
public function testCanModerateComments() public function testCanModerateComments()
{ {
// ensure nobody logged in // ensure nobody logged in
if (Member::currentUser()) { if (Security::getCurrentUser()) {
Member::currentUser()->logOut(); Security::getCurrentUser()->logOut();
} }
$item = $this->objFromFixture(CommentableItem::class, 'first'); $item = $this->objFromFixture(CommentableItem::class, 'first');

View File

@ -18,6 +18,7 @@ use SilverStripe\Forms\GridField\GridFieldDeleteAction;
use SilverStripe\ORM\ArrayList; use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\DataList; use SilverStripe\ORM\DataList;
use SilverStripe\ORM\DataObject; use SilverStripe\ORM\DataObject;
use SilverStripe\Dev\Deprecation;
class CommentsGridFieldActionTest extends SapphireTest class CommentsGridFieldActionTest extends SapphireTest
{ {
@ -40,6 +41,9 @@ class CommentsGridFieldActionTest extends SapphireTest
protected function setUp(): void protected function setUp(): void
{ {
parent::setUp(); parent::setUp();
if (Deprecation::isEnabled()) {
$this->markTestSkipped('Test calls deprecated code');
}
$this->list = new DataList(Team::class); $this->list = new DataList(Team::class);
$config = CommentsGridFieldConfig::create()->addComponent(new GridFieldDeleteAction()); $config = CommentsGridFieldConfig::create()->addComponent(new GridFieldDeleteAction());
$this->gridField = new CommentsGridField('testfield', 'testfield', $this->list, $config); $this->gridField = new CommentsGridField('testfield', 'testfield', $this->list, $config);

View File

@ -8,6 +8,7 @@ use SilverStripe\Dev\TestOnly;
use SilverStripe\ORM\DataObject; use SilverStripe\ORM\DataObject;
use SilverStripe\Security\Member; use SilverStripe\Security\Member;
use SilverStripe\Security\Permission; use SilverStripe\Security\Permission;
use SilverStripe\Security\Security;
class CommentableItem extends DataObject implements TestOnly class CommentableItem extends DataObject implements TestOnly
{ {
@ -39,7 +40,8 @@ class CommentableItem extends DataObject implements TestOnly
} elseif (is_numeric($member)) { } elseif (is_numeric($member)) {
$memberID = $member; $memberID = $member;
} else { } else {
$memberID = Member::currentUserID(); $currentUser = Security::getCurrentUser();
$memberID = $currentUser ? $currentUser->ID : 0;
} }
if ($memberID && Permission::checkMember($memberID, array('ADMIN', 'CMS_ACCESS_CommentAdmin'))) { if ($memberID && Permission::checkMember($memberID, array('ADMIN', 'CMS_ACCESS_CommentAdmin'))) {