1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace eNTiDi\Autotoc\Tests; |
4
|
|
|
|
5
|
|
|
use eNTiDi\Autotoc\Autotoc; |
6
|
|
|
use eNTiDi\Autotoc\Tests\TestObject; |
7
|
|
|
use SilverStripe\Core\Config\Config; |
8
|
|
|
use SilverStripe\Dev\SapphireTest; |
9
|
|
|
use SilverStripe\View\ArrayData; |
10
|
|
|
|
11
|
|
|
class AutotocTest extends SapphireTest |
12
|
|
|
{ |
13
|
|
|
public function setUp() |
14
|
|
|
{ |
15
|
|
|
parent::setUp(); |
16
|
|
|
|
17
|
|
|
// Enable the Autotoc extension on TestObject |
18
|
|
|
TestObject::add_extension('eNTiDi\Autotoc\Autotoc'); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
private function emptyTestObject() |
22
|
|
|
{ |
23
|
|
|
$obj = new TestObject; |
24
|
|
|
$obj->Content = ''; |
|
|
|
|
25
|
|
|
$obj->Test2 = ''; |
|
|
|
|
26
|
|
|
return $obj; |
27
|
|
|
} |
28
|
|
|
private function populatedTestObject() |
29
|
|
|
{ |
30
|
|
|
$obj = new TestObject; |
31
|
|
|
$obj->Content = file_get_contents(__DIR__ . '/test1'); |
|
|
|
|
32
|
|
|
$obj->Test2 = file_get_contents(__DIR__ . '/test2'); |
|
|
|
|
33
|
|
|
return $obj; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function testBodyAutotoc() |
37
|
|
|
{ |
38
|
|
|
$obj = new TestObject; |
39
|
|
|
$this->assertEquals(' data-spy="scroll" data-target=".toc"', $obj->getBodyAutotoc()); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function testContentField() |
43
|
|
|
{ |
44
|
|
|
$obj = new TestObject; |
45
|
|
|
$obj->Content = '<p>Content</p>'; |
|
|
|
|
46
|
|
|
$obj->Test2 = '<p>Test2</p>'; |
|
|
|
|
47
|
|
|
|
48
|
|
|
// Check the default content field is Content |
49
|
|
|
$this->assertEquals('<p>Content</p>', $obj->OriginalContentField); |
|
|
|
|
50
|
|
|
|
51
|
|
|
// Try to change the content field |
52
|
|
|
$obj->config()->update('content_field', 'Test2'); |
53
|
|
|
$this->assertEquals('<p>Test2</p>', $obj->OriginalContentField); |
|
|
|
|
54
|
|
|
|
55
|
|
|
// Change it again |
56
|
|
|
$obj->config()->update('content_field', 'Unexistent'); |
57
|
|
|
$this->assertEquals('', $obj->OriginalContentField); |
|
|
|
|
58
|
|
|
|
59
|
|
|
// Restore original value |
60
|
|
|
$obj->config()->update('content_field', 'Content'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testGetAutotoc() |
64
|
|
|
{ |
65
|
|
|
$obj = new TestObject; |
66
|
|
|
$toc = $obj->getAutotoc(); |
|
|
|
|
67
|
|
|
$this->assertNull($toc); |
68
|
|
|
|
69
|
|
|
$obj->Content = file_get_contents(__DIR__ . '/test1'); |
|
|
|
|
70
|
|
|
$obj->Test2 = file_get_contents(__DIR__ . '/test2'); |
|
|
|
|
71
|
|
|
|
72
|
|
|
// Old TOC should still be cached |
73
|
|
|
$toc = $obj->getAutotoc(); |
|
|
|
|
74
|
|
|
$this->assertNull($toc); |
75
|
|
|
|
76
|
|
|
$obj->clearAutotoc(); |
|
|
|
|
77
|
|
|
|
78
|
|
|
$toc = $obj->getAutotoc(); |
|
|
|
|
79
|
|
|
$this->assertTrue($toc instanceof ArrayData); |
80
|
|
|
$this->assertEquals(5, $toc->Children->count()); |
81
|
|
|
$this->assertStringEqualsFile(__DIR__ . '/test1', $obj->OriginalContentField); |
|
|
|
|
82
|
|
|
$this->assertStringEqualsFile(__DIR__ . '/html2', $obj->ContentField); |
|
|
|
|
83
|
|
|
$this->assertStringEqualsFile(__DIR__ . '/html2', $obj->Content); |
|
|
|
|
84
|
|
|
|
85
|
|
|
// Change the content field |
86
|
|
|
$obj->config()->update('content_field', 'Test2'); |
87
|
|
|
$obj->clearAutotoc(); |
|
|
|
|
88
|
|
|
|
89
|
|
|
$toc = $obj->getAutotoc(); |
|
|
|
|
90
|
|
|
$this->assertNull($toc); |
91
|
|
|
$this->assertStringEqualsFile(__DIR__ . '/test2', $obj->OriginalContentField); |
|
|
|
|
92
|
|
|
$this->assertStringEqualsFile(__DIR__ . '/test2', $obj->ContentField); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function testAugmentCallback() |
96
|
|
|
{ |
97
|
|
|
$obj = new TestObject; |
98
|
|
|
$obj->Content = file_get_contents(__DIR__ . '/test1'); |
|
|
|
|
99
|
|
|
$obj->Test2 = file_get_contents(__DIR__ . '/test2'); |
|
|
|
|
100
|
|
|
|
101
|
|
|
// Change the augmenter at class level |
102
|
|
|
Config::inst()->update( |
|
|
|
|
103
|
|
|
get_class($obj), |
104
|
|
|
'augment_callback', |
105
|
|
|
'eNTiDi\Autotoc\Tocifier::prependAnchor' |
106
|
|
|
); |
107
|
|
|
$obj->clearAutotoc(); |
|
|
|
|
108
|
|
|
|
109
|
|
|
$toc = $obj->getAutotoc(); |
|
|
|
|
110
|
|
|
$this->assertEquals(5, $toc->Children->count()); |
111
|
|
|
$this->assertStringEqualsFile(__DIR__ . '/html1', $obj->Content); |
|
|
|
|
112
|
|
|
|
113
|
|
|
// Change the augmenter at install level: should have higher |
114
|
|
|
// precedence |
115
|
|
|
$obj->config()->update( |
116
|
|
|
'augment_callback', |
117
|
|
|
'eNTiDi\Autotoc\Tocifier::setId' |
118
|
|
|
); |
119
|
|
|
$obj->clearAutotoc(); |
|
|
|
|
120
|
|
|
|
121
|
|
|
$toc = $obj->getAutotoc(); |
|
|
|
|
122
|
|
|
$this->assertEquals(5, $toc->Children->count()); |
123
|
|
|
$this->assertStringEqualsFile(__DIR__ . '/html2', $obj->Content); |
|
|
|
|
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.