Code Duplication    Length = 44-44 lines in 3 locations

tests/CommentsTest.php 3 locations

@@ 298-341 (lines=44) @@
295
        $this->assertFalse($check && $check->exists());
296
    }
297
298
    public function testSpamComment()
299
    {
300
        // Test anonymous user
301
        if ($member = Member::currentUser()) {
302
            $member->logOut();
303
        }
304
        $comment = $this->objFromFixture(Comment::class, 'firstComA');
305
        $commentID = $comment->ID;
306
        $this->assertNull($comment->SpamLink(), 'No permission to see mark as spam link');
307
        $spam = $this->get('comments/spam/'.$comment->ID.'?ajax=1');
308
        $this->assertEquals(403, $spam->getStatusCode());
309
        $check = DataObject::get_by_id(Comment::class, $commentID);
310
        $this->assertEquals(0, $check->IsSpam, 'No permission to mark as spam');
311
312
        // Test non-authenticated user
313
        $this->logInAs('visitor');
314
        $this->assertNull($comment->SpamLink(), 'No permission to see mark as spam link');
315
316
        // Test authenticated user
317
        $this->logInAs('commentadmin');
318
        $comment = $this->objFromFixture(Comment::class, 'firstComA');
319
        $commentID = $comment->ID;
320
        $adminComment1Link = $comment->SpamLink();
321
        $this->assertContains('comments/spam/' . $commentID . '?t=', $adminComment1Link);
322
323
        // Test that this link can't be shared / XSS exploited
324
        $this->logInAs('commentadmin2');
325
        $spam = $this->get($adminComment1Link);
326
        $this->assertEquals(400, $spam->getStatusCode());
327
        $check = DataObject::get_by_id(Comment::class, $comment->ID);
328
        $this->assertEquals(0, $check->IsSpam, 'No permission to mark as spam');
329
330
        // Test that this other admin can spam the comment with their own link
331
        $adminComment2Link = $comment->SpamLink();
332
        $this->assertNotEquals($adminComment2Link, $adminComment1Link);
333
        $this->autoFollowRedirection = false;
334
        $spam = $this->get($adminComment2Link);
335
        $this->assertEquals(302, $spam->getStatusCode());
336
        $check = DataObject::get_by_id(Comment::class, $commentID);
337
        $this->assertEquals(1, $check->IsSpam);
338
339
        // Cannot re-spam spammed comment
340
        $this->assertNull($check->SpamLink());
341
    }
342
343
    public function testHamComment()
344
    {
@@ 343-386 (lines=44) @@
340
        $this->assertNull($check->SpamLink());
341
    }
342
343
    public function testHamComment()
344
    {
345
        // Test anonymous user
346
        if ($member = Member::currentUser()) {
347
            $member->logOut();
348
        }
349
        $comment = $this->objFromFixture(Comment::class, 'secondComC');
350
        $commentID = $comment->ID;
351
        $this->assertNull($comment->HamLink(), 'No permission to see mark as ham link');
352
        $ham = $this->get('comments/ham/' . $comment->ID . '?ajax=1');
353
        $this->assertEquals(403, $ham->getStatusCode());
354
        $check = DataObject::get_by_id(Comment::class, $commentID);
355
        $this->assertEquals(1, $check->IsSpam, 'No permission to mark as ham');
356
357
        // Test non-authenticated user
358
        $this->logInAs('visitor');
359
        $this->assertNull($comment->HamLink(), 'No permission to see mark as ham link');
360
361
        // Test authenticated user
362
        $this->logInAs('commentadmin');
363
        $comment = $this->objFromFixture(Comment::class, 'secondComC');
364
        $commentID = $comment->ID;
365
        $adminComment1Link = $comment->HamLink();
366
        $this->assertContains('comments/ham/' . $commentID . '?t=', $adminComment1Link);
367
368
        // Test that this link can't be shared / XSS exploited
369
        $this->logInAs('commentadmin2');
370
        $ham = $this->get($adminComment1Link);
371
        $this->assertEquals(400, $ham->getStatusCode());
372
        $check = DataObject::get_by_id(Comment::class, $comment->ID);
373
        $this->assertEquals(1, $check->IsSpam, 'No permission to mark as ham');
374
375
        // Test that this other admin can ham the comment with their own link
376
        $adminComment2Link = $comment->HamLink();
377
        $this->assertNotEquals($adminComment2Link, $adminComment1Link);
378
        $this->autoFollowRedirection = false;
379
        $ham = $this->get($adminComment2Link);
380
        $this->assertEquals(302, $ham->getStatusCode());
381
        $check = DataObject::get_by_id(Comment::class, $commentID);
382
        $this->assertEquals(0, $check->IsSpam);
383
384
        // Cannot re-ham hammed comment
385
        $this->assertNull($check->HamLink());
386
    }
387
388
    public function testApproveComment()
389
    {
@@ 388-431 (lines=44) @@
385
        $this->assertNull($check->HamLink());
386
    }
387
388
    public function testApproveComment()
389
    {
390
        // Test anonymous user
391
        if ($member = Member::currentUser()) {
392
            $member->logOut();
393
        }
394
        $comment = $this->objFromFixture(Comment::class, 'secondComB');
395
        $commentID = $comment->ID;
396
        $this->assertNull($comment->ApproveLink(), 'No permission to see approve link');
397
        $approve = $this->get('comments/approve/' . $comment->ID . '?ajax=1');
398
        $this->assertEquals(403, $approve->getStatusCode());
399
        $check = DataObject::get_by_id(Comment::class, $commentID);
400
        $this->assertEquals(0, $check->Moderated, 'No permission to approve');
401
402
        // Test non-authenticated user
403
        $this->logInAs('visitor');
404
        $this->assertNull($comment->ApproveLink(), 'No permission to see approve link');
405
406
        // Test authenticated user
407
        $this->logInAs('commentadmin');
408
        $comment = $this->objFromFixture(Comment::class, 'secondComB');
409
        $commentID = $comment->ID;
410
        $adminComment1Link = $comment->ApproveLink();
411
        $this->assertContains('comments/approve/' . $commentID . '?t=', $adminComment1Link);
412
413
        // Test that this link can't be shared / XSS exploited
414
        $this->logInAs('commentadmin2');
415
        $approve = $this->get($adminComment1Link);
416
        $this->assertEquals(400, $approve->getStatusCode());
417
        $check = DataObject::get_by_id(Comment::class, $comment->ID);
418
        $this->assertEquals(0, $check->Moderated, 'No permission to approve');
419
420
        // Test that this other admin can approve the comment with their own link
421
        $adminComment2Link = $comment->ApproveLink();
422
        $this->assertNotEquals($adminComment2Link, $adminComment1Link);
423
        $this->autoFollowRedirection = false;
424
        $approve = $this->get($adminComment2Link);
425
        $this->assertEquals(302, $approve->getStatusCode());
426
        $check = DataObject::get_by_id(Comment::class, $commentID);
427
        $this->assertEquals(1, $check->Moderated);
428
429
        // Cannot re-approve approved comment
430
        $this->assertNull($check->ApproveLink());
431
    }
432
433
    public function testCommenterURLWrite()
434
    {