Completed
Pull Request — master (#59)
by
unknown
01:36
created

testGeneratesUniqueUrls()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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