ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
RootFolderTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
22 
27 final class RootFolderTest extends TestCase
28 {
29  private const ALLOWED_ROOT_FOLDER_FILES = [
30  '.babelrc.json',
31  '.eslintrc.json',
32  '.gitignore',
33  '.htaccess',
34  '.mocharc.json',
35  '.phpunit.result.cache',
36  'captainhook.local.json',
37  'phpstan.local.neon',
38  'phpstan-baseline.neon',
39  '.php_cs.cache',
40  '.php-cs-fixer.cache',
41  'calendar.php',
42  'captainhook.json',
43  'composer.json',
44  'composer.lock',
45  'confirmReg.php',
46  'error.php',
47  'favicon.ico',
48  'feed.php',
49  'goto.php',
50  'gs_content.php',
51  'ilias.ini.php',
52  'ilias.php',
53  'index.php',
54  'LICENSE',
55  'login.php',
56  'logout.php',
57  'lti.php',
58  'openidconnect.php',
59  'package-lock.json',
60  'package.json',
61  'privfeed.php',
62  'pwassist.php',
63  'README.md',
64  'register.php',
65  'rootindex.php',
66  'saml.php',
67  'sessioncheck.php',
68  'shib_login.php',
69  'shib_logout.php',
70  'storeScorm.php',
71  'storeScorm2004.php',
72  'studip_referrer.php',
73  'unzip_test_file.zip',
74  'webdav.php',
75  '.DS_Store',
76  '.buildpath',
77  '.project'
78  ];
79 
80  private const ALLOWED_ROOT_FOLDER_DIRS = [
81  '.git',
82  '.github',
83  '.idea',
84  'CI',
85  'Customizing',
86  'Modules',
87  'Services',
88  'cron',
89  'data',
90  'dicto',
91  'docs',
92  'extern',
93  'include',
94  'lang',
95  'libs',
96  'node_modules',
97  'setup',
98  'src',
99  'sso',
100  'templates',
101  'test',
102  'tests',
103  'webservice',
104  'wopi',
105  'xml',
106  '.settings'
107  ];
108 
109  protected array $ALLOWED_ROOT_FOLDER_DIRS = [];
110  protected array $ALLOWED_ROOT_FOLDER_FILES = [];
111 
112  protected function setUp(): void
113  {
114  $this->ALLOWED_ROOT_FOLDER_DIRS = array_merge(
115  self::ALLOWED_ROOT_FOLDER_DIRS,
116  explode(",", (string) getenv('ALLOWED_ROOT_FOLDER_DIRS'))
117  );
118  $this->ALLOWED_ROOT_FOLDER_FILES = array_merge(
119  self::ALLOWED_ROOT_FOLDER_FILES,
120  explode(",", (string) getenv('ALLOWED_ROOT_FOLDER_FILES'))
121  );
122  }
123 
124  private function getAppRootFolderOrFail(): string
125  {
126  $app_root_folder = getcwd();
127 
128  for ($i = 0; $i < 20 && !is_file($app_root_folder . '/index.php'); $i++) {
129  $app_root_folder .= '/..';
130  }
131 
132  if (!is_file($app_root_folder . '/index.php')) {
133  $this->fail('Could not determine ILIAS root folder');
134  }
135 
136  return $app_root_folder;
137  }
138 
139  public function testAppRootFolderOnlyContainsDefinedFiles(): void
140  {
141  $found_files = [];
142  $iter = new CallbackFilterIterator(
143  new DirectoryIterator($this->getAppRootFolderOrFail()),
144  static function (DirectoryIterator $file): bool {
145  return $file->isFile();
146  }
147  );
148  foreach ($iter as $file) {
150  $found_files[] = $file->getBasename();
151  }
152  sort($found_files);
153 
154  $unexpected_files = array_diff($found_files, $this->ALLOWED_ROOT_FOLDER_FILES);
155 
156  $this->assertEmpty(
157  $unexpected_files,
158  sprintf(
159  'The following files are not expected in the ILIAS root folder: %s',
160  implode(', ', $unexpected_files)
161  )
162  );
163  }
164 
165  public function testAppRootFolderOnlyContainsDefinedFolders(): void
166  {
167  $found_directories = [];
168  $iter = new CallbackFilterIterator(
169  new DirectoryIterator($this->getAppRootFolderOrFail()),
170  static function (DirectoryIterator $file): bool {
171  return $file->isDir() && !$file->isDot();
172  }
173  );
174  foreach ($iter as $file) {
176  $found_directories[] = $file->getBasename();
177  }
178 
179  $unexpected_directories = array_diff($found_directories, $this->ALLOWED_ROOT_FOLDER_DIRS);
180  sort($unexpected_directories);
181 
182  $this->assertEmpty(
183  $unexpected_directories,
184  sprintf(
185  'The following directories are not expected in the ILIAS root folder: %s',
186  implode(', ', $unexpected_directories)
187  )
188  );
189  }
190 }
const ALLOWED_ROOT_FOLDER_FILES
Class RootFolderTest.
const ALLOWED_ROOT_FOLDER_DIRS
array $ALLOWED_ROOT_FOLDER_DIRS
array $ALLOWED_ROOT_FOLDER_FILES