silverstripe-autotoc 

silverstripe-autotoc Commit Details

Date:2017-12-29 18:51:22 (3 years 9 months ago)
Author:Scrutinizer Auto-Fixer
Branch:dev, master
Commit:601c2f0b59200dbd49cb6f355932538cff86cdff
Parents: 2ac5a630b2c7aea6a2a114fa6f648fd63eec0c24
Message:Scrutinizer Auto-Fixes

This commit consists of patches automatically generated for this project on https://scrutinizer-ci.com
Changes:
Msrc/Autotoc.php (5 diffs)
Msrc/Hacks.php (1 diff)
Mtests/AutotocTest.php (6 diffs)
Mtests/TocifierTest.php (2 diffs)

File differences

src/Autotoc.php
8989
9090
9191
92
92
9393
9494
9595
......
103103
104104
105105
106
106
107107
108108
109109
......
122122
123123
124124
125
125
126126
127127
128128
......
150150
151151
152152
153
153
154154
155155
156156
......
171171
172172
173173
174
174
175175
176176
177
177
178178
179179
180180
181181
182
182
183183
184184
185185
Hacks::addCallbackMethodToInstance(
$owner,
'getContent',
function () use ($owner) {
function() use ($owner) {
return $owner->getContentField();
}
);
* (e.g. because of malformed content) no further attempts will be
* made.
*
* @param DataObject $owner
* @param \SilverStripe\ORM\DataObject $owner
* @return Tocifier|false|null
*/
private static function getTocifier($owner)
$callback = Config::inst()->get(self::class, 'augment_callback');
}
$tocifier->setAugmentCallback(explode('::', $callback));
if (! $tocifier->process()) {
if (!$tocifier->process()) {
$tocifier = false;
}
self::$tocifiers[$owner] = $tocifier;
public function getAutotoc()
{
$tocifier = self::getTocifier($this->owner);
if (! $tocifier) {
if (!$tocifier) {
return null;
}
public function getOriginalContentField()
{
$model = $this->owner->getCustomisedObj();
if (! $model) {
if (!$model) {
$model = $this->owner->data();
}
if (! $model) {
if (!$model) {
return null;
}
$field = $this->contentField();
if (! $model->hasField($field)) {
if (!$model->hasField($field)) {
return null;
}
src/Hacks.php
2626
2727
2828
29
30
31
32
2933
3034
3135
];
}
/**
* @param string $method
* @param \Closure $callback
*/
public static function addCallbackMethodToInstance($instance, $method, $callback)
{
// hasMethod() trigger the population of $extra_methods
tests/AutotocTest.php
2828
2929
3030
31
32
31
32
3333
3434
3535
......
6666
6767
6868
69
70
69
70
7171
7272
7373
......
7878
7979
8080
81
82
83
81
82
83
8484
8585
8686
......
8888
8989
9090
91
92
91
92
9393
9494
9595
9696
9797
98
99
98
99
100100
101101
102102
......
108108
109109
110110
111
111
112112
113113
114114
......
120120
121121
122122
123
123
124124
125125
private function populatedTestObject()
{
$obj = new TestObject;
$obj->Content = file_get_contents(__DIR__ . '/test1');
$obj->Test2 = file_get_contents(__DIR__ . '/test2');
$obj->Content = file_get_contents(__DIR__.'/test1');
$obj->Test2 = file_get_contents(__DIR__.'/test2');
return $obj;
}
$toc = $obj->getAutotoc();
$this->assertNull($toc);
$obj->Content = file_get_contents(__DIR__ . '/test1');
$obj->Test2 = file_get_contents(__DIR__ . '/test2');
$obj->Content = file_get_contents(__DIR__.'/test1');
$obj->Test2 = file_get_contents(__DIR__.'/test2');
// Old TOC should still be cached
$toc = $obj->getAutotoc();
$toc = $obj->getAutotoc();
$this->assertTrue($toc instanceof ArrayData);
$this->assertEquals(5, $toc->Children->count());
$this->assertStringEqualsFile(__DIR__ . '/test1', $obj->OriginalContentField);
$this->assertStringEqualsFile(__DIR__ . '/html2', $obj->ContentField);
$this->assertStringEqualsFile(__DIR__ . '/html2', $obj->Content);
$this->assertStringEqualsFile(__DIR__.'/test1', $obj->OriginalContentField);
$this->assertStringEqualsFile(__DIR__.'/html2', $obj->ContentField);
$this->assertStringEqualsFile(__DIR__.'/html2', $obj->Content);
// Change the content field
$obj->config()->update('content_field', 'Test2');
$toc = $obj->getAutotoc();
$this->assertNull($toc);
$this->assertStringEqualsFile(__DIR__ . '/test2', $obj->OriginalContentField);
$this->assertStringEqualsFile(__DIR__ . '/test2', $obj->ContentField);
$this->assertStringEqualsFile(__DIR__.'/test2', $obj->OriginalContentField);
$this->assertStringEqualsFile(__DIR__.'/test2', $obj->ContentField);
}
public function testAugmentCallback()
{
$obj = new TestObject;
$obj->Content = file_get_contents(__DIR__ . '/test1');
$obj->Test2 = file_get_contents(__DIR__ . '/test2');
$obj->Content = file_get_contents(__DIR__.'/test1');
$obj->Test2 = file_get_contents(__DIR__.'/test2');
// Change the augmenter at class level
Config::inst()->update(
$toc = $obj->getAutotoc();
$this->assertEquals(5, $toc->Children->count());
$this->assertStringEqualsFile(__DIR__ . '/html1', $obj->Content);
$this->assertStringEqualsFile(__DIR__.'/html1', $obj->Content);
// Change the augmenter at install level: should have higher
// precedence
$toc = $obj->getAutotoc();
$this->assertEquals(5, $toc->Children->count());
$this->assertStringEqualsFile(__DIR__ . '/html2', $obj->Content);
$this->assertStringEqualsFile(__DIR__.'/html2', $obj->Content);
}
}
tests/TocifierTest.php
2727
2828
2929
30
30
3131
3232
3333
3434
35
35
3636
3737
3838
3939
40
40
4141
4242
4343
4444
45
45
4646
4747
4848
4949
50
50
5151
5252
5353
......
5555
5656
5757
58
58
5959
6060
6161
6262
63
63
6464
6565
6666
6767
68
68
6969
7070
7171
public function testPrependAnchor()
{
$tocifier = new Tocifier(file_get_contents(__DIR__ . '/test1'));
$tocifier = new Tocifier(file_get_contents(__DIR__.'/test1'));
$this->assertEquals('', $tocifier->getHtml());
$tocifier->setAugmentCallback(array('\eNTiDi\Autotoc\Tocifier', 'prependAnchor'));
$this->assertTrue($tocifier->process());
$this->assertStringEqualsFile(__DIR__ . '/html1', $tocifier->getHtml());
$this->assertStringEqualsFile(__DIR__.'/html1', $tocifier->getHtml());
}
public function testSetId()
{
$tocifier = new Tocifier(file_get_contents(__DIR__ . '/test1'));
$tocifier = new Tocifier(file_get_contents(__DIR__.'/test1'));
$this->assertEquals('', $tocifier->getHtml());
// The default augmenting method should already be setId
$this->assertTrue($tocifier->process());
$this->assertStringEqualsFile(__DIR__ . '/html2', $tocifier->getHtml());
$this->assertStringEqualsFile(__DIR__.'/html2', $tocifier->getHtml());
}
public function testTOC()
{
$tocifier = new Tocifier(file_get_contents(__DIR__ . '/test1'));
$tocifier = new Tocifier(file_get_contents(__DIR__.'/test1'));
$this->assertEquals(array(), $tocifier->getTOC());
$this->assertTrue($tocifier->process());
$this->assertNotNull($tocifier->getTOC());
ob_start();
$tocifier->dumpTOC();
$returned = ob_get_clean();
$this->assertStringEqualsFile(__DIR__ . '/toc1', $returned);
$this->assertStringEqualsFile(__DIR__.'/toc1', $returned);
}
public function testDataHideFromTOC()
{
$tocifier = new Tocifier(file_get_contents(__DIR__ . '/test2'));
$tocifier = new Tocifier(file_get_contents(__DIR__.'/test2'));
$this->assertEquals('', $tocifier->getHtml());
$this->assertTrue($tocifier->process());
// Check the augmented HTML is equal to the original one
$this->assertStringEqualsFile(__DIR__ . '/test2', $tocifier->getHtml());
$this->assertStringEqualsFile(__DIR__.'/test2', $tocifier->getHtml());
ob_start();
$tocifier->dumpTOC();

Archive Download the corresponding diff file