|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\CMS\Tests\Behaviour; |
|
4
|
|
|
|
|
5
|
|
|
use Behat\Behat\Context\Context; |
|
|
|
|
|
|
6
|
|
|
use SilverStripe\BehatExtension\Context\MainContextAwareTrait; |
|
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Context used to create fixtures in the SilverStripe ORM. |
|
10
|
|
|
*/ |
|
11
|
|
|
class ThemeContext implements Context |
|
12
|
|
|
{ |
|
13
|
|
|
use MainContextAwareTrait; |
|
14
|
|
|
|
|
15
|
|
|
protected $restoreFiles = array(); |
|
16
|
|
|
protected $restoreDirectories = array(); |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Create a test theme |
|
20
|
|
|
* |
|
21
|
|
|
* @Given /^a theme "(?<theme>[^"]+)"/ |
|
22
|
|
|
* @param string $theme |
|
23
|
|
|
*/ |
|
24
|
|
|
public function stepCreateTheme($theme) |
|
25
|
|
|
{ |
|
26
|
|
|
if (!preg_match('/^[0-9a-zA-Z_-]+$/', $theme)) { |
|
27
|
|
|
throw new \InvalidArgumentException("Bad theme '$theme'"); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
$this->requireDir(BASE_PATH . '/themes'); |
|
31
|
|
|
$this->requireDir(BASE_PATH . '/themes/' . $theme); |
|
32
|
|
|
$this->requireDir(BASE_PATH . '/themes/' . $theme . '/templates'); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Create a template within a test theme |
|
37
|
|
|
* |
|
38
|
|
|
* @Given /^a template "(?<template>[^"]+)" in theme "(?<theme>[^"]+)" with content "(?<content>[^"]+)"/ |
|
39
|
|
|
* @param string $template |
|
40
|
|
|
* @param string $theme |
|
41
|
|
|
* @param string $content |
|
42
|
|
|
*/ |
|
43
|
|
|
public function stepCreateTemplate($template, $theme, $content) |
|
44
|
|
|
{ |
|
45
|
|
|
if (!preg_match('/^[0-9a-zA-Z_-]+$/', $theme)) { |
|
46
|
|
|
throw new \InvalidArgumentException("Bad theme '$theme'"); |
|
47
|
|
|
} |
|
48
|
|
|
if (!preg_match('/^(Layout\/)?[0-9a-zA-Z_-]+\.ss$/', $template)) { |
|
49
|
|
|
throw new \InvalidArgumentException("Bad template '$template'"); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$this->stepCreateTheme($theme); |
|
53
|
|
|
$this->requireFile(BASE_PATH . '/themes/' . $theme . '/templates/' . $template, $content); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
protected function requireFile($filename, $content) |
|
57
|
|
|
{ |
|
58
|
|
|
// Already exists |
|
59
|
|
|
if (file_exists($filename)) { |
|
60
|
|
|
// If the content is different, remember old content for restoration |
|
61
|
|
|
$origContent = file_get_contents($filename); |
|
62
|
|
|
if ($origContent != $content) { |
|
63
|
|
|
file_put_contents($filename, $content); |
|
64
|
|
|
$this->restoreFiles[$filename] = $origContent; |
|
65
|
|
|
} |
|
66
|
|
|
// Doesn't exist, mark it for deletion after test |
|
67
|
|
|
} else { |
|
68
|
|
|
file_put_contents($filename, $content); |
|
69
|
|
|
$this->restoreFiles[$filename] = null; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
protected function requireDir($dirname) |
|
74
|
|
|
{ |
|
75
|
|
|
// Directory doesn't exist, create it and mark it for deletion |
|
76
|
|
|
if (!file_exists($dirname)) { |
|
77
|
|
|
mkdir($dirname); |
|
78
|
|
|
$this->restoreDirectories[] = $dirname; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Clean up any theme manipulation |
|
84
|
|
|
* |
|
85
|
|
|
* @AfterScenario |
|
86
|
|
|
*/ |
|
87
|
|
|
public function cleanThemesAfterScenario() |
|
88
|
|
|
{ |
|
89
|
|
|
// Restore any created/modified files. |
|
90
|
|
|
// - If modified, revert then to original contnet |
|
91
|
|
|
// - If created, delete them |
|
92
|
|
|
if ($this->restoreFiles) { |
|
|
|
|
|
|
93
|
|
|
foreach ($this->restoreFiles as $file => $origContent) { |
|
94
|
|
|
if ($origContent === null) { |
|
95
|
|
|
unlink($file); |
|
96
|
|
|
} else { |
|
97
|
|
|
file_put_contents($file, $origContent); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
$this->restoreFiles = array(); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
// Restore any created directories: that is, delete them |
|
105
|
|
|
if ($this->restoreDirectories) { |
|
|
|
|
|
|
106
|
|
|
// Flip the order so that nested direcotires are unlinked() first |
|
107
|
|
|
$this->restoreDirectories = array_reverse($this->restoreDirectories); |
|
108
|
|
|
foreach ($this->restoreDirectories as $dir) { |
|
109
|
|
|
rmdir($dir); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
$this->restoreDirectories = array(); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths