Passed
Push — master ( b4f0a0...3f9085 )
by
unknown
06:23 queued 04:24
created

testSchemaIsAddedToAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace SilverStripe\TagField\Tests;
4
5
use PHPUnit_Framework_TestCase;
6
use SilverStripe\Control\HTTPRequest;
7
use SilverStripe\Dev\SapphireTest;
8
use SilverStripe\Forms\FieldList;
9
use SilverStripe\Forms\Form;
10
use SilverStripe\TagField\StringTagField;
11
use SilverStripe\TagField\Tests\Stub\StringTagFieldTestBlogPost;
12
13
/**
14
 * @mixin PHPUnit_Framework_TestCase
15
 */
16
class StringTagFieldTest extends SapphireTest
17
{
18
    /**
19
     * @var string
20
     */
21
    protected static $fixture_file = 'StringTagFieldTest.yml';
22
23
    /**
24
     * @var array
25
     */
26
    protected static $extra_dataobjects = [
27
        StringTagFieldTestBlogPost::class,
28
    ];
29
30
    public function testItSavesTagsOnNewRecords()
31
    {
32
        $record = $this->getNewStringTagFieldTestBlogPost('BlogPost1');
33
34
        $field = new StringTagField('Tags');
35
        $field->setValue(['Tag1', 'Tag2']);
36
        $field->saveInto($record);
37
38
        $record->write();
39
40
        $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...
41
    }
42
43
    /**
44
     * @param string $name
45
     *
46
     * @return StringTagFieldTestBlogPost
47
     */
48
    protected function getNewStringTagFieldTestBlogPost($name)
49
    {
50
        return $this->objFromFixture(
51
            StringTagFieldTestBlogPost::class,
52
            $name
53
        );
54
    }
55
56
    public function testItSavesTagsOnExistingRecords()
57
    {
58
        $record = $this->getNewStringTagFieldTestBlogPost('BlogPost1');
59
        $record->write();
60
61
        $field = new StringTagField('Tags');
62
        $field->setValue(['Tag1', 'Tag2']);
63
        $field->saveInto($record);
64
65
        $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...
66
    }
67
68
    public function testItSuggestsTags()
69
    {
70
        $field = new StringTagField('SomeField', 'Some field', ['Tag1', 'Tag2'], []);
71
72
        /**
73
         * Partial tag title match.
74
         */
75
        $request = $this->getNewRequest(['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(['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(['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(['term' => 'unknown']);
103
104
        $this->assertEquals(
105
            '{"items":[]}',
106
            $field->suggest($request)->getBody()
107
        );
108
    }
109
110
    public function testGetSchemaDataDefaults()
111
    {
112
        $form = new Form(null, 'Form', new FieldList(), new FieldList());
113
        $field = new StringTagField('TestField', 'Test Field', ['one', 'two']);
114
        $field->setForm($form);
115
116
        $field
117
            ->setShouldLazyLoad(false)
118
            ->setCanCreate(false);
119
120
        $schema = $field->getSchemaDataDefaults();
121
        $this->assertSame('TestField[]', $schema['name']);
122
        $this->assertFalse($schema['lazyLoad']);
123
        $this->assertFalse($schema['creatable']);
124
        $this->assertEquals([
125
            ['Title' => 'one', 'Value' => 'one'],
126
            ['Title' => 'two', 'Value' => 'two'],
127
        ], $schema['options']);
128
129
        $field
130
            ->setShouldLazyLoad(true)
131
            ->setCanCreate(true);
132
133
        $schema = $field->getSchemaDataDefaults();
134
        $this->assertTrue($schema['lazyLoad']);
135
        $this->assertTrue($schema['creatable']);
136
        $this->assertContains('suggest', $schema['optionUrl']);
137
    }
138
139
    public function testSchemaIsAddedToAttributes()
140
    {
141
        $field = new StringTagField('TestField');
142
        $attributes = $field->getAttributes();
143
        $this->assertNotEmpty($attributes['data-schema']);
144
    }
145
146
    /**
147
     * @param array $parameters
148
     * @return HTTPRequest
149
     */
150
    protected function getNewRequest(array $parameters)
151
    {
152
        return new HTTPRequest(
153
            'get',
154
            'StringTagFieldTestController/StringTagFieldTestForm/fields/Tags/suggest',
155
            $parameters
156
        );
157
    }
158
}
159