Completed
Pull Request — master (#106)
by
unknown
03:26
created

StringTagFieldTest::getNewRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
namespace SilverStripe\TagField\Tests;
4
5
use SilverStripe\Control\HTTPRequest;
6
use SilverStripe\Dev\SapphireTest;
7
use SilverStripe\TagField\StringTagField;
8
use SilverStripe\TagField\Tests\Stub\StringTagFieldTestBlogPost;
9
10
/**
11
 * @mixin PHPUnit_Framework_TestCase
12
 */
13
class StringTagFieldTest extends SapphireTest
14
{
15
    /**
16
     * @var string
17
     */
18
    protected static $fixture_file = 'StringTagFieldTest.yml';
19
20
    /**
21
     * @var array
22
     */
23
    protected static $extra_dataobjects = array(
24
        StringTagFieldTestBlogPost::class,
25
    );
26
27
    public function testItSavesTagsOnNewRecords()
28
    {
29
        $record = $this->getNewStringTagFieldTestBlogPost('BlogPost1');
30
31
        $field = new StringTagField('Tags');
32
        $field->setValue(array('Tag1', 'Tag2'));
33
        $field->saveInto($record);
34
35
        $record->write();
36
37
        $this->assertEquals('Tag1,Tag2', $record->Tags);
0 ignored issues
show
Bug Best Practice introduced by
The property Tags does not exist on SilverStripe\TagField\Te...ingTagFieldTestBlogPost. Since you implemented __get, consider adding a @property annotation.
Loading history...
38
    }
39
40
    /**
41
     * @param string $name
42
     *
43
     * @return StringTagFieldTestBlogPost
44
     */
45
    protected function getNewStringTagFieldTestBlogPost($name)
46
    {
47
        return $this->objFromFixture(
48
            StringTagFieldTestBlogPost::class,
49
            $name
50
        );
51
    }
52
53
    public function testItSavesTagsOnExistingRecords()
54
    {
55
        $record = $this->getNewStringTagFieldTestBlogPost('BlogPost1');
56
        $record->write();
57
58
        $field = new StringTagField('Tags');
59
        $field->setValue(array('Tag1', 'Tag2'));
60
        $field->saveInto($record);
61
62
        $this->assertEquals('Tag1,Tag2', $record->Tags);
0 ignored issues
show
Bug Best Practice introduced by
The property Tags does not exist on SilverStripe\TagField\Te...ingTagFieldTestBlogPost. Since you implemented __get, consider adding a @property annotation.
Loading history...
63
    }
64
65
    public function testItSuggestsTags()
66
    {
67
        $record = $this->getNewStringTagFieldTestBlogPost('BlogPost2');
68
69
        $field = new StringTagField('Tags');
70
        $field->setRecord($record);
71
72
        /**
73
         * Partial tag title match.
74
         */
75
        $request = $this->getNewRequest(array('term' => 'Tag'));
76
77
        $this->assertEquals(
78
            '{"items":[{"id":"Tag1","text":"Tag1"},{"id":"Tag2","text":"Tag2"}]}',
79
            $field->suggest($request)->getBody()
80
        );
81
82
        /**
83
         * Exact tag title match.
84
         */
85
        $request = $this->getNewRequest(array('term' => 'Tag1'));
86
87
        $this->assertEquals($field->suggest($request)->getBody(), '{"items":[{"id":"Tag1","text":"Tag1"}]}');
88
89
        /**
90
         * Case-insensitive tag title match.
91
         */
92
        $request = $this->getNewRequest(array('term' => 'TAG1'));
93
94
        $this->assertEquals(
95
            '{"items":[{"id":"Tag1","text":"Tag1"}]}',
96
            $field->suggest($request)->getBody()
97
        );
98
99
        /**
100
         * No tag title match.
101
         */
102
        $request = $this->getNewRequest(array('term' => 'unknown'));
103
104
        $this->assertEquals(
105
            '{"items":[]}',
106
            $field->suggest($request)->getBody()
107
        );
108
    }
109
110
    /**
111
     * @param array $parameters
112
     *
113
     * @return HTTPRequest
114
     */
115
    protected function getNewRequest(array $parameters)
116
    {
117
        return new HTTPRequest(
118
            'get',
119
            'StringTagFieldTestController/StringTagFieldTestForm/fields/Tags/suggest',
120
            $parameters
121
        );
122
    }
123
}
124