Code Duplication    Length = 44-44 lines in 3 locations

tests/CommentsTest.php 3 locations

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