CommentNotifierTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 5
eloc 52
c 4
b 0
f 0
dl 0
loc 99
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testSendEmail() 0 56 1
A tearDown() 0 5 1
A testOnAfterPostComment() 0 11 1
A setUp() 0 7 2
1
<?php
2
3
namespace SilverStripe\CommentNotifications\Tests;
4
5
use SilverStripe\Dev\SapphireTest;
6
use SilverStripe\Control\Email\Email;
7
use SilverStripe\Core\Config\Config;
8
use SilverStripe\Security\Member;
9
use SilverStripe\Comments\Model\Comment;
10
use SilverStripe\CommentNotifications\Tests\Control\CommentNotifierTestController;
11
use SilverStripe\CommentNotifications\Tests\Model\CommentNotifiableTestDataObject;
12
13
class CommentNotifierTest extends SapphireTest
14
{
15
    protected static $fixture_file = 'CommentNotifications.yml';
16
17
    protected $oldhost = null;
18
19
    protected static $extra_dataobjects = [
20
        CommentNotifiableTestDataObject::class
21
    ];
22
23
    protected static $extra_controllers = [
24
        CommentNotifierTestController::class
25
    ];
26
27
    protected function setUp()
28
    {
29
        parent::setUp();
30
31
        Config::modify()->set('Email', 'admin_email', '[email protected]');
32
        $this->oldhost = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : null;
33
        $_SERVER['HTTP_HOST'] = 'www.mysite.com';
34
    }
35
36
    protected function tearDown()
37
    {
38
        $_SERVER['HTTP_HOST'] = $this->oldhost;
39
40
        parent::tearDown();
41
    }
42
43
    public function testSendEmail()
44
    {
45
        $author = $this->objFromFixture(Member::class, 'author');
46
        $item1 = $this->objFromFixture(CommentNotifiableTestDataObject::class, 'item1');
47
        $item2 = $this->objFromFixture(CommentNotifiableTestDataObject::class, 'item2');
48
        $comment1 = $this->objFromFixture(Comment::class, 'comment1');
49
        $comment2 = $this->objFromFixture(Comment::class, 'comment2');
50
        $comment3 = $this->objFromFixture(Comment::class, 'comment3');
51
        $controller = new CommentNotifierTestController();
52
53
        // Comment 1
54
        $result = $controller->notifyCommentRecipient($comment1, $item1, $author);
0 ignored issues
show
Bug introduced by
The method notifyCommentRecipient() does not exist on SilverStripe\CommentNoti...tNotifierTestController. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

54
        /** @scrutinizer ignore-call */ 
55
        $result = $controller->notifyCommentRecipient($comment1, $item1, $author);
Loading history...
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
55
        $this->assertEmailSent('[email protected]', '[email protected]', 'A new comment has been posted');
56
57
        $email = $this->findEmail('[email protected]', '[email protected]', 'A new comment has been posted');
58
59
        $this->assertContains('<li>Bob Bobberson</li>', $email['Content']);
60
        $this->assertContains('<li>[email protected]</li>', $email['Content']);
61
        $this->assertContains('<blockquote>Hey what a lovely comment</blockquote>', $email['Content']);
62
63
        $this->clearEmails();
64
65
        // Comment 2
66
        $result = $controller->notifyCommentRecipient($comment2, $item2, $author);
67
        $this->assertEmailSent('[email protected]', '[email protected]', 'A new comment has been posted');
68
69
        $email = $this->findEmail('[email protected]', '[email protected]', 'A new comment has been posted');
70
        $this->assertContains('<li>Secret</li>', $email['Content']);
71
        $this->assertContains('<li>[email protected]</li>', $email['Content']);
72
        $this->assertContains('<blockquote>I don&#039;t want to disclose my details</blockquote>', $email['Content']);
73
74
        $this->clearEmails();
75
76
        // Comment 3
77
        $result = $controller->notifyCommentRecipient($comment3, $item1, $author);
78
        $this->assertEmailSent('[email protected]', '[email protected]', 'A new comment has been posted');
79
80
        $email = $this->findEmail('[email protected]', '[email protected]', 'A new comment has been posted');
81
82
        $this->assertContains('<li>Anonymous</li>', $email['Content']);
83
        $this->assertContains('<li>[email protected]</li>', $email['Content']);
84
        $this->assertContains('<blockquote>I didn&#039;t log in</blockquote>', $email['Content']);
85
86
        $this->clearEmails();
87
88
        // Comment 3 without an author
89
        $result = $controller->notifyCommentRecipient($comment3, $item1, '[email protected]');
90
        $this->assertEmailSent('[email protected]', '[email protected]', 'A new comment has been posted');
91
92
        $this->clearEmails();
93
94
        // Comment 3 without a valid email
95
        $result = $controller->notifyCommentRecipient($comment3, $item1, '<foobar1>');
96
        $noEmail = (bool) $this->findEmail('<foobar1>', '[email protected]', 'A new comment has been posted');
97
98
        $this->assertFalse($noEmail);
99
    }
100
101
    public function testOnAfterPostComment()
102
    {
103
        $this->clearEmails();
104
105
        $comment1 = $this->objFromFixture(Comment::class, 'comment1');
106
107
        $controller = new CommentNotifierTestController();
108
        $controller->invokeWithExtensions('onAfterPostComment', $comment1);
109
110
        // test that after posting a comment the notifications are sent.
111
        $this->assertEmailSent('[email protected]', '[email protected]', 'A new comment has been posted');
112
    }
113
}
114