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'; |
|
|
|
|
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); |
|
|
|
|
23
|
|
|
$this->assertEquals('test-1', $term->URLSegment); |
|
|
|
|
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function testGeneratesUniqueUrls() |
27
|
|
|
{ |
28
|
|
|
$term1 = new TaxonomyTerm(); |
29
|
|
|
$term1->Name = 'Testing'; |
|
|
|
|
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); |
|
|
|
|
40
|
|
|
$this->assertEquals('testing-2', $term2->URLSegment); |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testRespectsSuppliedUrl() |
44
|
|
|
{ |
45
|
|
|
$term = new TaxonomyTerm(); |
46
|
|
|
$term->Name = 'Test 1'; |
|
|
|
|
47
|
|
|
$term->URLSegment = 'something supplied'; |
|
|
|
|
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); |
|
|
|
|
54
|
|
|
$this->assertEquals('something supplied', $term->URLSegment); |
|
|
|
|
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'; |
|
|
|
|
70
|
|
|
$term1->URLSegment = 'supplied-test'; |
|
|
|
|
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); |
|
|
|
|
82
|
|
|
$this->assertEquals('supplied-test-2', $term2->URLSegment); |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function testGeneratesUrlWhenNameIsInvalid() |
86
|
|
|
{ |
87
|
|
|
$term = new TaxonomyTerm(); |
88
|
|
|
$term->Name = '##'; |
|
|
|
|
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); |
|
|
|
|
95
|
|
|
$this->assertEquals('term-1', $term->URLSegment); |
|
|
|
|
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
|
|
|
|