Passed
Pull Request — master (#59)
by
unknown
01:29
created

testGeneratesUniqueUrlIfSuppliedIsUsed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 0
dl 0
loc 17
rs 9.8666
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\ORM\DataObject;
8
9
class TaxonomyTermUrlExtensionTest extends SapphireTest
10
{
11
    protected static $fixture_file = 'TaxonomyTermUrlExtensionTest.yml';
12
    protected $usesDatabase = true;
13
14
    public function testGeneratesUrl()
15
    {
16
        $term = new TaxonomyTerm();
17
        $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...
18
        $term->write();
19
20
        // Reload the model
21
        $term = DataObject::get_by_id(TaxonomyTerm::class, $term->ID);
22
        $this->assertNotNull($term);
23
        $this->assertEquals('Test 1', $term->Name);
24
        $this->assertEquals('test-1', $term->URLSegment);
25
    }
26
27
    public function testGeneratesUniqueUrls()
28
    {
29
        $term1 = new TaxonomyTerm();
30
        $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...
31
        $term1->write();
32
33
        $term2 = new TaxonomyTerm();
34
        $term2->Name = 'Testing'; // intentionally the same
35
        $term2->write();
36
37
        // Reload the model
38
        $term2 = DataObject::get_by_id(TaxonomyTerm::class, $term2->ID);
39
        $this->assertNotNull($term2);
40
        $this->assertEquals('Testing', $term2->Name);
41
        $this->assertEquals('testing-2', $term2->URLSegment);
42
    }
43
44
    public function testRespectsSuppliedUrl()
45
    {
46
        $term = new TaxonomyTerm();
47
        $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...
48
        $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...
49
        $term->write();
50
51
        // Reload the model
52
        $term = DataObject::get_by_id(TaxonomyTerm::class, $term->ID);
53
        $this->assertNotNull($term);
54
        $this->assertEquals('Test 1', $term->Name);
55
        $this->assertEquals('something supplied', $term->URLSegment);
56
57
        // Check it doesn't overwrite if saved multiple times
58
        $term->Name = 'Test changed';
59
        $term->write();
60
61
        $term = DataObject::get_by_id(TaxonomyTerm::class, $term->ID);
62
        $this->assertNotNull($term);
63
        $this->assertEquals('Test changed', $term->Name);
64
        $this->assertEquals('something supplied', $term->URLSegment);
65
    }
66
67
    public function testGeneratesUniqueUrlIfSuppliedIsUsed()
68
    {
69
        $term1 = new TaxonomyTerm();
70
        $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...
71
        $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...
72
        $term1->write();
73
74
        $term2 = new TaxonomyTerm();
75
        $term2->Name = 'Test 2';
76
        $term2->URLSegment = 'supplied-test'; // intentionally the same
77
        $term2->write();
78
79
        // Reload the model
80
        $term2 = DataObject::get_by_id(TaxonomyTerm::class, $term2->ID);
81
        $this->assertNotNull($term2);
82
        $this->assertEquals('Test 2', $term2->Name);
83
        $this->assertEquals('supplied-test-2', $term2->URLSegment);
84
    }
85
86
    public function testGeneratesUrlWhenNameIsInvalid()
87
    {
88
        $term = new TaxonomyTerm();
89
        $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...
90
        $term->write();
91
92
        // Reload the model
93
        $term = DataObject::get_by_id(TaxonomyTerm::class, $term->ID);
94
        $this->assertNotNull($term);
95
        $this->assertEquals('##', $term->Name);
96
        $this->assertEquals('term-1', $term->URLSegment);
97
98
        $term = new TaxonomyTerm();
99
        $term->Name = ' ';
100
        $term->write();
101
102
        // Reload the model
103
        $term = DataObject::get_by_id(TaxonomyTerm::class, $term->ID);
104
        $this->assertNotNull($term);
105
        $this->assertEquals(' ', $term->Name);
106
        $this->assertEquals('term-2', $term->URLSegment);
107
    }
108
}
109