mirror of
https://github.com/silverstripe/silverstripe-comments
synced 2024-10-22 11:05:49 +02:00
ENHANCEMENT: dont allow comments to be displayed on fake pages such as security. BUGFIX: updated commentadmin tests to just work
This commit is contained in:
parent
d9580ed600
commit
5dec31c84f
@ -27,7 +27,7 @@ class CommentAdmin extends LeftAndMain {
|
||||
/**
|
||||
* @var int The number of comments per page for the {@link CommentTable} in this admin.
|
||||
*/
|
||||
static $comments_per_page = '20';
|
||||
static $comments_per_page = 20;
|
||||
|
||||
public function init() {
|
||||
parent::init();
|
||||
@ -78,18 +78,18 @@ class CommentAdmin extends LeftAndMain {
|
||||
"Name" => _t('CommentAdmin.AUTHOR', 'Author'),
|
||||
"Comment" => _t('CommentAdmin.COMMENT', 'Comment'),
|
||||
"Parent.Title" => _t('CommentAdmin.PAGE', 'Page'),
|
||||
"CommenterURL" => _t('CommentAdmin.COMMENTERURL', 'URL'),
|
||||
"URL" => _t('CommentAdmin.COMMENTERURL', 'URL'),
|
||||
"Created" => _t('CommentAdmin.DATEPOSTED', 'Date Posted')
|
||||
);
|
||||
|
||||
$popupFields = new FieldSet(
|
||||
new TextField('Name', _t('CommentAdmin.NAME', 'Name')),
|
||||
new TextField('CommenterURL', _t('CommentAdmin.COMMENTERURL', 'URL')),
|
||||
new TextField('URL', _t('CommentAdmin.URL', 'URL')),
|
||||
new TextareaField('Comment', _t('CommentAdmin.COMMENT', 'Comment'))
|
||||
);
|
||||
|
||||
$idField = new HiddenField('ID', '', $section);
|
||||
$table = new CommentTableField($this, "Comments", "PageComment", $section, $tableFields, $popupFields, array($filter), 'Created DESC');
|
||||
$table = new CommentTableField($this, "Comments", "Comment", $section, $tableFields, $popupFields, array($filter), 'Created DESC');
|
||||
|
||||
$table->setParentClass(false);
|
||||
$table->setFieldCasting(array(
|
||||
@ -139,8 +139,8 @@ class CommentAdmin extends LeftAndMain {
|
||||
|
||||
if($_REQUEST['Comments']) {
|
||||
foreach($_REQUEST['Comments'] as $commentid) {
|
||||
$comment = DataObject::get_by_id('PageComment', $commentid);
|
||||
if($comment) {
|
||||
$comment = DataObject::get_by_id('Comment', $commentid);
|
||||
if($comment && $comment->canDelete()) {
|
||||
$comment->delete();
|
||||
$numComments++;
|
||||
}
|
||||
@ -158,15 +158,17 @@ JS;
|
||||
|
||||
function deleteall() {
|
||||
$numComments = 0;
|
||||
$spam = DataObject::get('PageComment', '"PageComment"."IsSpam" = 1');
|
||||
$spam = DataObject::get('Comment', "\"Comment\".\"IsSpam\" = '1'");
|
||||
|
||||
if($spam) {
|
||||
$numComments = $spam->Count();
|
||||
|
||||
foreach($spam as $comment) {
|
||||
if($comment->canDelete()) {
|
||||
$comment->delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$msg = sprintf(_t('CommentAdmin.DELETED', 'Deleted %s comments.'), $numComments);
|
||||
echo <<<JS
|
||||
@ -183,23 +185,12 @@ JS;
|
||||
|
||||
if($_REQUEST['Comments']) {
|
||||
foreach($_REQUEST['Comments'] as $commentid) {
|
||||
$comment = DataObject::get_by_id('PageComment', $commentid);
|
||||
$comment = DataObject::get_by_id('Comment', $commentid);
|
||||
if($comment) {
|
||||
$comment->IsSpam = true;
|
||||
$comment->NeedsModeration = false;
|
||||
$comment->Moderated = true;
|
||||
$comment->write();
|
||||
|
||||
if(SSAkismet::isEnabled()) {
|
||||
try {
|
||||
$akismet = new SSAkismet();
|
||||
$akismet->setCommentAuthor($comment->getField('Name'));
|
||||
$akismet->setCommentContent($comment->getField('Comment'));
|
||||
|
||||
$akismet->submitSpam();
|
||||
} catch (Exception $e) {
|
||||
// Akismet didn't work, most likely the service is down.
|
||||
}
|
||||
}
|
||||
$numComments++;
|
||||
}
|
||||
}
|
||||
@ -222,24 +213,13 @@ JS;
|
||||
|
||||
if($_REQUEST['Comments']) {
|
||||
foreach($_REQUEST['Comments'] as $commentid) {
|
||||
$comment = DataObject::get_by_id('PageComment', $commentid);
|
||||
$comment = DataObject::get_by_id('Comment', $commentid);
|
||||
|
||||
if($comment) {
|
||||
$comment->IsSpam = false;
|
||||
$comment->NeedsModeration = false;
|
||||
$comment->Moderated = true;
|
||||
$comment->write();
|
||||
|
||||
if(SSAkismet::isEnabled()) {
|
||||
try {
|
||||
$akismet = new SSAkismet();
|
||||
$akismet->setCommentAuthor($comment->getField('Name'));
|
||||
$akismet->setCommentContent($comment->getField('Comment'));
|
||||
|
||||
$akismet->submitSpam();
|
||||
} catch (Exception $e) {
|
||||
// Akismet didn't work, most likely the service is down.
|
||||
}
|
||||
}
|
||||
|
||||
$numComments++;
|
||||
}
|
||||
}
|
||||
@ -262,10 +242,10 @@ JS;
|
||||
|
||||
if($_REQUEST['Comments']) {
|
||||
foreach($_REQUEST['Comments'] as $commentid) {
|
||||
$comment = DataObject::get_by_id('PageComment', $commentid);
|
||||
$comment = DataObject::get_by_id('Comment', $commentid);
|
||||
if($comment) {
|
||||
$comment->IsSpam = false;
|
||||
$comment->NeedsModeration = false;
|
||||
$comment->Moderated = true;
|
||||
$comment->write();
|
||||
$numComments++;
|
||||
}
|
||||
@ -284,27 +264,33 @@ JS;
|
||||
|
||||
/**
|
||||
* Return the number of moderated comments
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
function NumModerated() {
|
||||
return DB::query("SELECT COUNT(*) FROM \"PageComment\" WHERE \"IsSpam\"=0 AND \"NeedsModeration\"=0")->value();
|
||||
return DB::query("SELECT COUNT(*) FROM \"Comment\" WHERE \"Moderated\" = 1")->value();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the number of unmoderated comments
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
function NumUnmoderated() {
|
||||
return DB::query("SELECT COUNT(*) FROM \"PageComment\" WHERE \"IsSpam\"=0 AND \"NeedsModeration\"=1")->value();
|
||||
return DB::query("SELECT COUNT(*) FROM \"Comment\" WHERE \"Moderated\" = 0")->value();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the number of comments marked as spam
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
function NumSpam() {
|
||||
return DB::query("SELECT COUNT(*) FROM \"PageComment\" WHERE \"IsSpam\"=1")->value();
|
||||
return DB::query("SELECT COUNT(*) FROM \"Comment\" WHERE \"IsSpam\" = 1")->value();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $num int
|
||||
* @param int
|
||||
*/
|
||||
function set_comments_per_page($num){
|
||||
self::$comments_per_page = $num;
|
||||
@ -317,5 +303,3 @@ JS;
|
||||
return self::$comments_per_page;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
@ -13,7 +13,8 @@ class Comment extends DataObject {
|
||||
"Email" => "Varchar(200)",
|
||||
"URL" => "Varchar(255)",
|
||||
"BaseClass" => "Varchar(200)",
|
||||
"Moderated" => "Boolean"
|
||||
"Moderated" => "Boolean",
|
||||
"IsSpam" => "Boolean"
|
||||
);
|
||||
|
||||
static $has_one = array(
|
||||
|
@ -76,6 +76,9 @@ class CommentsExtension extends DataObjectDecorator {
|
||||
// trigger comments on / off via ProvideComments
|
||||
$enabled = (!$this->attachedToSiteTree() || $this->owner->ProvideComments) ? true : false;
|
||||
|
||||
// do not include the comments on pages which don't have id's such as security pages
|
||||
if($this->owner->ID < 0) return false;
|
||||
|
||||
$controller = new CommentingController();
|
||||
|
||||
// tad bit messy but needed to ensure all datas available
|
||||
@ -85,6 +88,7 @@ class CommentsExtension extends DataObjectDecorator {
|
||||
|
||||
$form = ($enabled) ? $controller->CommentsForm() : false;
|
||||
|
||||
Debug::show(Controller::curr()->ID);
|
||||
// a little bit all over the show but to ensure a slightly easier upgrade for users
|
||||
// return back the same variables as previously done in comments
|
||||
return $interface->process(new ArrayData(array(
|
||||
|
39
tests/CommentAdminTest.php
Normal file
39
tests/CommentAdminTest.php
Normal file
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package comments
|
||||
*/
|
||||
|
||||
class CommentAdminTest extends FunctionalTest {
|
||||
|
||||
static $fixture_file = 'comments/tests/CommentsTest.yml';
|
||||
|
||||
function testNumModerated() {
|
||||
$comm = new CommentAdmin();
|
||||
$resp = $comm->NumModerated();
|
||||
$this->assertEquals(4, $resp);
|
||||
}
|
||||
|
||||
function testNumUnmoderated(){
|
||||
$comm = new CommentAdmin();
|
||||
$resp = $comm->NumUnmoderated();
|
||||
$this->assertEquals(3, $resp);
|
||||
}
|
||||
|
||||
function testNumSpam(){
|
||||
$comm = new CommentAdmin();
|
||||
$resp = $comm->NumSpam();
|
||||
$this->assertEquals(2, $resp);
|
||||
}
|
||||
|
||||
function testdeletemarked(){
|
||||
$comm = $this->objFromFixture('Comment', 'firstComA');
|
||||
$id = $comm->ID;
|
||||
$this->logInWithPermission('CMS_ACCESS_CommentAdmin');
|
||||
$result = $this->get("admin/comments/EditForm/field/Comments/item/$id/delete");
|
||||
|
||||
$checkComm = DataObject::get_by_id('Comment',$id);
|
||||
|
||||
$this->assertFalse($checkComm);
|
||||
}
|
||||
}
|
@ -37,26 +37,36 @@ Comment:
|
||||
ParentID: =>Page.first
|
||||
Name: FA
|
||||
Comment: textFA
|
||||
Moderated: 1
|
||||
secondComA:
|
||||
ParentID: =>Page.second
|
||||
Name: SA
|
||||
Comment: textSA
|
||||
Moderated: 1
|
||||
secondComB:
|
||||
ParentID: =>Page.second
|
||||
Name: SB
|
||||
Comment: textSB
|
||||
Moderated: 1
|
||||
thirdComA:
|
||||
ParentID: =>Page.third
|
||||
Name: TA
|
||||
Comment: textTA
|
||||
Moderated: 1
|
||||
IsSpam: 1
|
||||
thirdComB:
|
||||
ParentID: =>Page.third
|
||||
Name: TB
|
||||
Comment: textTB
|
||||
Moderated: 0
|
||||
thirdComC:
|
||||
ParentID: =>Page.third
|
||||
Name: TC
|
||||
Comment: textTC
|
||||
Moderated: 0
|
||||
|
||||
disabledCom:
|
||||
ParentID: =>Page.pageNoComments
|
||||
Name: Disabled
|
||||
Moderated: 0
|
||||
IsSpam: 1
|
Loading…
Reference in New Issue
Block a user