Completed
Push — master ( e42a4c...f187a0 )
by Daniel
03:17
created

tests/CommentListTest.php (6 issues)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
class CommentListTest extends FunctionalTest
4
{
5
6
    public static $fixture_file = 'comments/tests/CommentsTest.yml';
7
8
    protected $extraDataObjects = array(
9
        'CommentableItem',
10
        'CommentableItemEnabled',
11
        'CommentableItemDisabled'
12
    );
13
14 View Code Duplication
    public function setUp()
15
    {
16
        parent::setUp();
17
        Config::nest();
18
19
        // Set good default values
20
        Config::inst()->update('CommentsExtension', 'comments', array(
21
            'enabled' => true,
22
            'enabled_cms' => false,
23
            'require_login' => false,
24
            'require_login_cms' => false,
25
            'required_permission' => false,
26
            'require_moderation_nonmembers' => false,
27
            'require_moderation' => false,
28
            'require_moderation_cms' => false,
29
            'frontend_moderation' => false,
30
            'frontend_spam' => false,
31
        ));
32
33
        // Configure this dataobject
34
        Config::inst()->update('CommentableItem', 'comments', array(
35
            'enabled_cms' => true
36
        ));
37
    }
38
39
    public function tearDown()
40
    {
41
        Config::unnest();
42
        parent::tearDown();
43
    }
44
45
    public function testGetForeignClass()
46
    {
47
        $item = $this->objFromFixture('CommentableItem', 'first');
48
49
        // This is the class the Comments are related to
50
        $this->assertEquals('CommentableItem',
51
                                $item->Comments()->getForeignClass());
52
    }
53
54 View Code Duplication
    public function testAddNonComment()
55
    {
56
        $item = $this->objFromFixture('CommentableItem', 'first');
57
        $comments = $item->Comments();
58
        $this->assertEquals(4, $comments->count());
0 ignored issues
show
The method assertEquals() does not seem to exist on object<CommentListTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
59
        $member = Member::get()->first();
60
        try {
61
            $comments->add($member);
62
            $this->fail('Should not have been able to add member to comments');
63
        } catch (InvalidArgumentException $e) {
64
            $this->assertEquals(
0 ignored issues
show
The method assertEquals() does not seem to exist on object<CommentListTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
65
                'CommentList::add() expecting a Comment object, or ID value',
66
                $e->getMessage()
67
            );
68
        }
69
    }
70
71
    public function testAddComment()
72
    {
73
        $item = $this->objFromFixture('CommentableItem', 'first');
74
        $firstComment = $this->objFromFixture('Comment', 'firstComA');
75
        $comments = $item->Comments();//->sort('Created');
76
77
        foreach ($comments as $comment) {
78
            error_log($comment->ID . ' ' . $comment->Created .' ' . $comment->Comment);
79
        }
80
81
        $this->assertEquals(4, $comments->count());
0 ignored issues
show
The method assertEquals() does not seem to exist on object<CommentListTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
82
        $newComment = new Comment();
83
        $newComment->Name = 'Fred Bloggs';
84
        $newComment->Comment = 'This is a test comment';
85
        $newComment->write();
86
87
        $comments->add($newComment);
88
        // As a comment has been added, there should be 5 comments now
89
        $this->assertEquals(5, $item->Comments()->count());
90
91
        $newComment2 = new Comment();
92
        $newComment2->Name = 'John Smith';
93
        $newComment2->Comment = 'This is another test comment';
94
        $newComment2->write();
95
96
        // test adding the same comment by ID
97
        $comments->add($newComment2->ID);
98
        $this->assertEquals(6, $item->Comments()->count());
99
100
        $this->setExpectedException(
101
            'InvalidArgumentException',
102
            "CommentList::add() can't be called until a single foreign ID is set"
103
        );
104
        $list = new CommentList('CommentableItem');
105
        $list->add($newComment);
106
    }
107
108
    public function testRemoveComment()
109
    {
110
        // remove by comment
111
        $item = $this->objFromFixture('CommentableItem', 'first');
112
        $this->assertEquals(4, $item->Comments()->count());
0 ignored issues
show
The method assertEquals() does not seem to exist on object<CommentListTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
113
        $comments = $item->Comments();
114
        $comment = $comments->first();
115
        $comments->remove($comment);
116
117
        // now remove by ID
118
        $comments = $item->Comments();
119
        $comment = $comments->first();
120
        $comments->remove($comment->ID);
121
        $this->assertEquals(2, $item->Comments()->count());
122
    }
123
124 View Code Duplication
    public function testRemoveNonComment()
125
    {
126
        $item = $this->objFromFixture('CommentableItem', 'first');
127
        $this->assertEquals(4, $item->Comments()->count());
0 ignored issues
show
The method assertEquals() does not seem to exist on object<CommentListTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
128
        $comments = $item->Comments();
129
130
        // try and remove a non comment
131
        $member = Member::get()->first();
132
133
134
135
        try {
136
            $comments->remove($member);
137
            $this->fail('Should not have been able to remove member from comments');
138
        } catch (InvalidArgumentException $e) {
139
            $this->assertEquals(
0 ignored issues
show
The method assertEquals() does not seem to exist on object<CommentListTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
140
                'CommentList::remove() expecting a Comment object, or ID',
141
                $e->getMessage()
142
            );
143
        }
144
    }
145
}
146