Passed
Pull Request — master (#59)
by
unknown
01:38
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
15
    public static function setUpBeforeClass()
16
    {
17
        parent::setUpBeforeClass();
18
        Extensible::add_extension(TaxonomyTerm::class, TaxonomyTermUrlExtension::class);
19
    }
20
21
    public function testGeneratesUrl()
22
    {
23
        $term = new TaxonomyTerm();
24
        $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...
25
        $term->write();
26
27
        // Reload the model
28
        $term = DataObject::get_by_id(TaxonomyTerm::class, $term->ID);
29
        $this->assertNotNull($term);
30
        $this->assertEquals('Test 1', $term->Name);
31
        $this->assertEquals('test-1', $term->URLSegment);
32
    }
33
34
    public function testGeneratesUniqueUrls()
35
    {
36
        $term1 = new TaxonomyTerm();
37
        $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...
38
        $term1->write();
39
40
        $term2 = new TaxonomyTerm();
41
        $term2->Name = 'Testing'; // intentionally the same
42
        $term2->write();
43
44
        // Reload the model
45
        $term2 = DataObject::get_by_id(TaxonomyTerm::class, $term2->ID);
46
        $this->assertNotNull($term2);
47
        $this->assertEquals('Testing', $term2->Name);
48
        $this->assertEquals('testing-2', $term2->URLSegment);
49
    }
50
51
    public function testRespectsSuppliedUrl()
52
    {
53
        $term = new TaxonomyTerm();
54
        $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...
55
        $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...
56
        $term->write();
57
58
        // Reload the model
59
        $term = DataObject::get_by_id(TaxonomyTerm::class, $term->ID);
60
        $this->assertNotNull($term);
61
        $this->assertEquals('Test 1', $term->Name);
62
        $this->assertEquals('something supplied', $term->URLSegment);
63
64
        // Check it doesn't overwrite if saved multiple times
65
        $term->Name = 'Test changed';
66
        $term->write();
67
68
        $term = DataObject::get_by_id(TaxonomyTerm::class, $term->ID);
69
        $this->assertNotNull($term);
70
        $this->assertEquals('Test changed', $term->Name);
71
        $this->assertEquals('something supplied', $term->URLSegment);
72
    }
73
74
    public function testGeneratesUniqueUrlIfSuppliedIsUsed()
75
    {
76
        $term1 = new TaxonomyTerm();
77
        $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...
78
        $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...
79
        $term1->write();
80
81
        $term2 = new TaxonomyTerm();
82
        $term2->Name = 'Test 2';
83
        $term2->URLSegment = 'supplied-test'; // intentionally the same
84
        $term2->write();
85
86
        // Reload the model
87
        $term2 = DataObject::get_by_id(TaxonomyTerm::class, $term2->ID);
88
        $this->assertNotNull($term2);
89
        $this->assertEquals('Test 2', $term2->Name);
90
        $this->assertEquals('supplied-test-2', $term2->URLSegment);
91
    }
92
93
    public function testGeneratesUrlWhenNameIsInvalid()
94
    {
95
        $term = new TaxonomyTerm();
96
        $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...
97
        $term->write();
98
99
        // Reload the model
100
        $term = DataObject::get_by_id(TaxonomyTerm::class, $term->ID);
101
        $this->assertNotNull($term);
102
        $this->assertEquals('##', $term->Name);
103
        $this->assertEquals('term-1', $term->URLSegment);
104
105
        $term = new TaxonomyTerm();
106
        $term->Name = ' ';
107
        $term->write();
108
109
        // Reload the model
110
        $term = DataObject::get_by_id(TaxonomyTerm::class, $term->ID);
111
        $this->assertNotNull($term);
112
        $this->assertEquals(' ', $term->Name);
113
        $this->assertEquals('term-2', $term->URLSegment);
114
    }
115
}
116