Passed
Pull Request — master (#59)
by
unknown
03:05
created

testRespectsSuppliedUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 14
nc 1
nop 0
dl 0
loc 21
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\Taxonomy\Tests;
4
5
use SilverStripe\Dev\SapphireTest;
6
use SilverStripe\Taxonomy\TaxonomyTerm;
7
use SilverStripe\Taxonomy\Extensions\TaxonomyTermUrlExtension;
8
use SilverStripe\ORM\DataObject;
9
use SilverStripe\Core\Extensible;
10
11
class TaxonomyTermUrlExtensionTest extends SapphireTest
12
{
13
    protected $usesDatabase = true;
14
    protected static $required_extensions = [TaxonomyTerm::class => [TaxonomyTermUrlExtension::class]];
15
16
    public function testGeneratesUrl()
17
    {
18
        $term = new TaxonomyTerm();
19
        $term->Name = 'Test 1';
0 ignored issues
show
Bug Best Practice introduced by
The property Name does not exist on SilverStripe\Taxonomy\TaxonomyTerm. Since you implemented __set, consider adding a @property annotation.
Loading history...
20
        $term->write();
21
22
        // Reload the model
23
        $term = DataObject::get_by_id(TaxonomyTerm::class, $term->ID);
24
        $this->assertNotNull($term);
25
        $this->assertEquals('Test 1', $term->Name);
26
        $this->assertEquals('test-1', $term->URLSegment);
27
    }
28
29
    public function testGeneratesUniqueUrls()
30
    {
31
        $term1 = new TaxonomyTerm();
32
        $term1->Name = 'Testing';
0 ignored issues
show
Bug Best Practice introduced by
The property Name does not exist on SilverStripe\Taxonomy\TaxonomyTerm. Since you implemented __set, consider adding a @property annotation.
Loading history...
33
        $term1->write();
34
35
        $term2 = new TaxonomyTerm();
36
        $term2->Name = 'Testing'; // intentionally the same
37
        $term2->write();
38
39
        // Reload the model
40
        $term2 = DataObject::get_by_id(TaxonomyTerm::class, $term2->ID);
41
        $this->assertNotNull($term2);
42
        $this->assertEquals('Testing', $term2->Name);
43
        $this->assertEquals('testing-2', $term2->URLSegment);
44
    }
45
46
    public function testRespectsSuppliedUrl()
47
    {
48
        $term = new TaxonomyTerm();
49
        $term->Name = 'Test 1';
0 ignored issues
show
Bug Best Practice introduced by
The property Name does not exist on SilverStripe\Taxonomy\TaxonomyTerm. Since you implemented __set, consider adding a @property annotation.
Loading history...
50
        $term->URLSegment = 'something supplied';
0 ignored issues
show
Bug Best Practice introduced by
The property URLSegment does not exist on SilverStripe\Taxonomy\TaxonomyTerm. Since you implemented __set, consider adding a @property annotation.
Loading history...
51
        $term->write();
52
53
        // Reload the model
54
        $term = DataObject::get_by_id(TaxonomyTerm::class, $term->ID);
55
        $this->assertNotNull($term);
56
        $this->assertEquals('Test 1', $term->Name);
57
        $this->assertEquals('something supplied', $term->URLSegment);
58
59
        // Check it doesn't overwrite if saved multiple times
60
        $term->Name = 'Test changed';
61
        $term->write();
62
63
        $term = DataObject::get_by_id(TaxonomyTerm::class, $term->ID);
64
        $this->assertNotNull($term);
65
        $this->assertEquals('Test changed', $term->Name);
66
        $this->assertEquals('something supplied', $term->URLSegment);
67
    }
68
69
    public function testGeneratesUniqueUrlIfSuppliedIsUsed()
70
    {
71
        $term1 = new TaxonomyTerm();
72
        $term1->Name = 'Test 1';
0 ignored issues
show
Bug Best Practice introduced by
The property Name does not exist on SilverStripe\Taxonomy\TaxonomyTerm. Since you implemented __set, consider adding a @property annotation.
Loading history...
73
        $term1->URLSegment = 'supplied-test';
0 ignored issues
show
Bug Best Practice introduced by
The property URLSegment does not exist on SilverStripe\Taxonomy\TaxonomyTerm. Since you implemented __set, consider adding a @property annotation.
Loading history...
74
        $term1->write();
75
76
        $term2 = new TaxonomyTerm();
77
        $term2->Name = 'Test 2';
78
        $term2->URLSegment = 'supplied-test'; // intentionally the same
79
        $term2->write();
80
81
        // Reload the model
82
        $term2 = DataObject::get_by_id(TaxonomyTerm::class, $term2->ID);
83
        $this->assertNotNull($term2);
84
        $this->assertEquals('Test 2', $term2->Name);
85
        $this->assertEquals('supplied-test-2', $term2->URLSegment);
86
    }
87
88
    public function testGeneratesUrlWhenNameIsInvalid()
89
    {
90
        $term = new TaxonomyTerm();
91
        $term->Name = '##';
0 ignored issues
show
Bug Best Practice introduced by
The property Name does not exist on SilverStripe\Taxonomy\TaxonomyTerm. Since you implemented __set, consider adding a @property annotation.
Loading history...
92
        $term->write();
93
94
        // Reload the model
95
        $term = DataObject::get_by_id(TaxonomyTerm::class, $term->ID);
96
        $this->assertNotNull($term);
97
        $this->assertEquals('##', $term->Name);
98
        $this->assertEquals('term-1', $term->URLSegment);
99
100
        $term = new TaxonomyTerm();
101
        $term->Name = ' ';
102
        $term->write();
103
104
        // Reload the model
105
        $term = DataObject::get_by_id(TaxonomyTerm::class, $term->ID);
106
        $this->assertNotNull($term);
107
        $this->assertEquals(' ', $term->Name);
108
        $this->assertEquals('term-2', $term->URLSegment);
109
    }
110
}
111