/src/test/php/org/bovigo/vfs/vfsStreamWrapperDirTestCase.php

https://github.com/acoulton/vfsStream · PHP · 543 lines · 319 code · 48 blank · 176 comment · 30 complexity · db86c5c51ede095cbba8d2721afb0bc2 MD5 · raw file

  1. <?php
  2. /**
  3. * Test for org::bovigo::vfs::vfsStreamWrapper around mkdir().
  4. *
  5. * @package bovigo_vfs
  6. * @subpackage test
  7. */
  8. require_once 'org/bovigo/vfs/vfsStream.php';
  9. require_once 'PHPUnit/Framework/TestCase.php';
  10. require_once dirname(__FILE__) . '/vfsStreamWrapperBaseTestCase.php';
  11. /**
  12. * Test for org::bovigo::vfs::vfsStreamWrapper around mkdir().
  13. *
  14. * @package bovigo_vfs
  15. * @subpackage test
  16. */
  17. class vfsStreamWrapperMkDirTestCase extends vfsStreamWrapperBaseTestCase
  18. {
  19. /**
  20. * mkdir() should not overwrite existing root
  21. *
  22. * @test
  23. */
  24. public function mkdirNoNewRoot()
  25. {
  26. $this->assertFalse(mkdir(vfsStream::url('another')));
  27. $this->assertEquals(2, count($this->foo->getChildren()));
  28. $this->assertSame($this->foo, vfsStreamWrapper::getRoot());
  29. }
  30. /**
  31. * mkdir() should not overwrite existing root
  32. *
  33. * @test
  34. */
  35. public function mkdirNoNewRootRecursively()
  36. {
  37. $this->assertFalse(mkdir(vfsStream::url('another/more'), 0777, true));
  38. $this->assertEquals(2, count($this->foo->getChildren()));
  39. $this->assertSame($this->foo, vfsStreamWrapper::getRoot());
  40. }
  41. /**
  42. * assert that mkdir() creates the correct directory structure
  43. *
  44. * @test
  45. * @group permissions
  46. */
  47. public function mkdirNonRecursively()
  48. {
  49. $this->assertFalse(mkdir($this->barURL . '/another/more'));
  50. $this->assertEquals(2, count($this->foo->getChildren()));
  51. $this->assertTrue(mkdir($this->fooURL . '/another'));
  52. $this->assertEquals(3, count($this->foo->getChildren()));
  53. $this->assertEquals(0777, $this->foo->getChild('another')->getPermissions());
  54. }
  55. /**
  56. * assert that mkdir() creates the correct directory structure
  57. *
  58. * @test
  59. * @group permissions
  60. */
  61. public function mkdirRecursively()
  62. {
  63. $this->assertTrue(mkdir($this->fooURL . '/another/more', 0777, true));
  64. $this->assertEquals(3, count($this->foo->getChildren()));
  65. $another = $this->foo->getChild('another');
  66. $this->assertTrue($another->hasChild('more'));
  67. $this->assertEquals(0777, $this->foo->getChild('another')->getPermissions());
  68. $this->assertEquals(0777, $this->foo->getChild('another')->getChild('more')->getPermissions());
  69. }
  70. /**
  71. * @test
  72. * @group issue_9
  73. * @since 0.9.0
  74. */
  75. public function mkdirWithDots()
  76. {
  77. $this->assertTrue(mkdir($this->fooURL . '/another/../more/.', 0777, true));
  78. $this->assertEquals(3, count($this->foo->getChildren()));
  79. $this->assertTrue($this->foo->hasChild('more'));
  80. }
  81. /**
  82. * no root > new directory becomes root
  83. *
  84. * @test
  85. * @group permissions
  86. */
  87. public function mkdirWithoutRootCreatesNewRoot()
  88. {
  89. vfsStreamWrapper::register();
  90. $this->assertTrue(@mkdir(vfsStream::url('foo')));
  91. $this->assertEquals(vfsStreamContent::TYPE_DIR, vfsStreamWrapper::getRoot()->getType());
  92. $this->assertEquals('foo', vfsStreamWrapper::getRoot()->getName());
  93. $this->assertEquals(0777, vfsStreamWrapper::getRoot()->getPermissions());
  94. }
  95. /**
  96. * trying to create a subdirectory of a file should not work
  97. *
  98. * @test
  99. */
  100. public function mkdirOnFileReturnsFalse()
  101. {
  102. $this->assertFalse(mkdir($this->baz1URL . '/another/more', 0777, true));
  103. }
  104. /**
  105. * assert that mkdir() creates the correct directory structure
  106. *
  107. * @test
  108. * @group permissions
  109. */
  110. public function mkdirNonRecursivelyDifferentPermissions()
  111. {
  112. $this->assertTrue(mkdir($this->fooURL . '/another', 0755));
  113. $this->assertEquals(0755, $this->foo->getChild('another')->getPermissions());
  114. }
  115. /**
  116. * assert that mkdir() creates the correct directory structure
  117. *
  118. * @test
  119. * @group permissions
  120. */
  121. public function mkdirRecursivelyDifferentPermissions()
  122. {
  123. $this->assertTrue(mkdir($this->fooURL . '/another/more', 0755, true));
  124. $this->assertEquals(3, count($this->foo->getChildren()));
  125. $another = $this->foo->getChild('another');
  126. $this->assertTrue($another->hasChild('more'));
  127. $this->assertEquals(0755, $this->foo->getChild('another')->getPermissions());
  128. $this->assertEquals(0755, $this->foo->getChild('another')->getChild('more')->getPermissions());
  129. }
  130. /**
  131. * assert that mkdir() creates the correct directory structure
  132. *
  133. * @test
  134. * @group permissions
  135. */
  136. public function mkdirRecursivelyUsesDefaultPermissions()
  137. {
  138. $this->foo->chmod(0700);
  139. $this->assertTrue(mkdir($this->fooURL . '/another/more', 0777, true));
  140. $this->assertEquals(3, count($this->foo->getChildren()));
  141. $another = $this->foo->getChild('another');
  142. $this->assertTrue($another->hasChild('more'));
  143. $this->assertEquals(0777, $this->foo->getChild('another')->getPermissions());
  144. $this->assertEquals(0777, $this->foo->getChild('another')->getChild('more')->getPermissions());
  145. }
  146. /**
  147. * no root > new directory becomes root
  148. *
  149. * @test
  150. * @group permissions
  151. */
  152. public function mkdirWithoutRootCreatesNewRootDifferentPermissions()
  153. {
  154. vfsStreamWrapper::register();
  155. $this->assertTrue(@mkdir(vfsStream::url('foo'), 0755));
  156. $this->assertEquals(vfsStreamContent::TYPE_DIR, vfsStreamWrapper::getRoot()->getType());
  157. $this->assertEquals('foo', vfsStreamWrapper::getRoot()->getName());
  158. $this->assertEquals(0755, vfsStreamWrapper::getRoot()->getPermissions());
  159. }
  160. /**
  161. * no root > new directory becomes root
  162. *
  163. * @test
  164. * @group permissions
  165. */
  166. public function mkdirWithoutRootCreatesNewRootWithDefaultPermissions()
  167. {
  168. vfsStreamWrapper::register();
  169. $this->assertTrue(@mkdir(vfsStream::url('foo')));
  170. $this->assertEquals(vfsStreamContent::TYPE_DIR, vfsStreamWrapper::getRoot()->getType());
  171. $this->assertEquals('foo', vfsStreamWrapper::getRoot()->getName());
  172. $this->assertEquals(0777, vfsStreamWrapper::getRoot()->getPermissions());
  173. }
  174. /**
  175. * @test
  176. * @group permissions
  177. * @group bug_15
  178. */
  179. public function mkdirDirCanNotCreateNewDirInNonWritingDirectory()
  180. {
  181. vfsStreamWrapper::register();
  182. vfsStreamWrapper::setRoot(new vfsStreamDirectory('root'));
  183. vfsStreamWrapper::getRoot()->addChild(new vfsStreamDirectory('restrictedFolder', 0000));
  184. $this->assertFalse(is_writable(vfsStream::url('root/restrictedFolder/')));
  185. $this->assertFalse(mkdir(vfsStream::url('root/restrictedFolder/newFolder')));
  186. $this->assertFalse(vfsStreamWrapper::getRoot()->hasChild('restrictedFolder/newFolder'));
  187. }
  188. /**
  189. * @test
  190. * @group permissions
  191. * @group bug_15
  192. */
  193. public function canNotIterateOverNonReadableDirectory()
  194. {
  195. vfsStreamWrapper::register();
  196. vfsStreamWrapper::setRoot(new vfsStreamDirectory('root', 0000));
  197. $this->assertFalse(@opendir(vfsStream::url('root')));
  198. $this->assertFalse(@dir(vfsStream::url('root')));
  199. }
  200. /**
  201. * @test
  202. */
  203. public function directoryIteration()
  204. {
  205. $dir = dir($this->fooURL);
  206. $i = 0;
  207. while (false !== ($entry = $dir->read())) {
  208. $i++;
  209. $this->assertTrue('bar' === $entry || 'baz2' === $entry);
  210. }
  211. $this->assertEquals(2, $i, 'Directory foo contains two children, but got ' . $i . ' children while iterating over directory contents');
  212. $dir->rewind();
  213. $i = 0;
  214. while (false !== ($entry = $dir->read())) {
  215. $i++;
  216. $this->assertTrue('bar' === $entry || 'baz2' === $entry);
  217. }
  218. $this->assertEquals(2, $i, 'Directory foo contains two children, but got ' . $i . ' children while iterating over directory contents');
  219. $dir->close();
  220. }
  221. /**
  222. * @test
  223. */
  224. public function directoryIterationWithDot()
  225. {
  226. $dir = dir($this->fooURL . '/.');
  227. $i = 0;
  228. while (false !== ($entry = $dir->read())) {
  229. $i++;
  230. $this->assertTrue('bar' === $entry || 'baz2' === $entry);
  231. }
  232. $this->assertEquals(2, $i, 'Directory foo contains two children, but got ' . $i . ' children while iterating over directory contents');
  233. $dir->rewind();
  234. $i = 0;
  235. while (false !== ($entry = $dir->read())) {
  236. $i++;
  237. $this->assertTrue('bar' === $entry || 'baz2' === $entry);
  238. }
  239. $this->assertEquals(2, $i, 'Directory foo contains two children, but got ' . $i . ' children while iterating over directory contents');
  240. $dir->close();
  241. }
  242. /**
  243. * assure that a directory iteration works as expected
  244. *
  245. * @test
  246. * @group regression
  247. * @group bug_2
  248. */
  249. public function directoryIterationWithOpenDir_Bug_2()
  250. {
  251. $handle = opendir($this->fooURL);
  252. $i = 0;
  253. while (false !== ($entry = readdir($handle))) {
  254. $i++;
  255. $this->assertTrue('bar' === $entry || 'baz2' === $entry);
  256. }
  257. $this->assertEquals(2, $i, 'Directory foo contains two children, but got ' . $i . ' children while iterating over directory contents');
  258. rewind($handle);
  259. $i = 0;
  260. while (false !== ($entry = readdir($handle))) {
  261. $i++;
  262. $this->assertTrue('bar' === $entry || 'baz2' === $entry);
  263. }
  264. $this->assertEquals(2, $i, 'Directory foo contains two children, but got ' . $i . ' children while iterating over directory contents');
  265. closedir($handle);
  266. }
  267. /**
  268. * assure that a directory iteration works as expected
  269. *
  270. * @author Christoph Bloemer
  271. * @test
  272. * @group regression
  273. * @group bug_4
  274. */
  275. public function directoryIteration_Bug_4()
  276. {
  277. $dir = $this->fooURL;
  278. $list1 = array();
  279. if ($handle = opendir($dir)) {
  280. while (false !== ($listItem = readdir($handle))) {
  281. if ('.' != $listItem && '..' != $listItem) {
  282. if (is_file($dir . '/' . $listItem) === true) {
  283. $list1[] = 'File:[' . $listItem . ']';
  284. } elseif (is_dir($dir . '/' . $listItem) === true) {
  285. $list1[] = 'Folder:[' . $listItem . ']';
  286. }
  287. }
  288. }
  289. closedir($handle);
  290. }
  291. $list2 = array();
  292. if ($handle = opendir($dir)) {
  293. while (false !== ($listItem = readdir($handle))) {
  294. if ('.' != $listItem && '..' != $listItem) {
  295. if (is_file($dir . '/' . $listItem) === true) {
  296. $list2[] = 'File:[' . $listItem . ']';
  297. } elseif (is_dir($dir . '/' . $listItem) === true) {
  298. $list2[] = 'Folder:[' . $listItem . ']';
  299. }
  300. }
  301. }
  302. closedir($handle);
  303. }
  304. $this->assertEquals($list1, $list2);
  305. $this->assertEquals(2, count($list1));
  306. $this->assertEquals(2, count($list2));
  307. }
  308. /**
  309. * assure that a directory iteration works as expected
  310. *
  311. * @test
  312. */
  313. public function directoryIterationShouldBeIndependent()
  314. {
  315. $list1 = array();
  316. $list2 = array();
  317. $handle1 = opendir($this->fooURL);
  318. if (false !== ($listItem = readdir($handle1))) {
  319. $list1[] = $listItem;
  320. }
  321. $handle2 = opendir($this->fooURL);
  322. if (false !== ($listItem = readdir($handle2))) {
  323. $list2[] = $listItem;
  324. }
  325. if (false !== ($listItem = readdir($handle1))) {
  326. $list1[] = $listItem;
  327. }
  328. if (false !== ($listItem = readdir($handle2))) {
  329. $list2[] = $listItem;
  330. }
  331. closedir($handle1);
  332. closedir($handle2);
  333. $this->assertEquals($list1, $list2);
  334. $this->assertEquals(2, count($list1));
  335. $this->assertEquals(2, count($list2));
  336. }
  337. /**
  338. * assert is_dir() returns correct result
  339. *
  340. * @test
  341. */
  342. public function is_dir()
  343. {
  344. $this->assertTrue(is_dir($this->fooURL));
  345. $this->assertTrue(is_dir($this->fooURL . '/.'));
  346. $this->assertTrue(is_dir($this->barURL));
  347. $this->assertTrue(is_dir($this->barURL . '/.'));
  348. $this->assertFalse(is_dir($this->baz1URL));
  349. $this->assertFalse(is_dir($this->baz2URL));
  350. $this->assertFalse(is_dir($this->fooURL . '/another'));
  351. $this->assertFalse(is_dir(vfsStream::url('another')));
  352. }
  353. /**
  354. * can not unlink without root
  355. *
  356. * @test
  357. */
  358. public function canNotUnlinkDirectoryWithoutRoot()
  359. {
  360. vfsStreamWrapper::register();
  361. $this->assertFalse(@rmdir(vfsStream::url('foo')));
  362. }
  363. /**
  364. * rmdir() can not remove files
  365. *
  366. * @test
  367. */
  368. public function rmdirCanNotRemoveFiles()
  369. {
  370. $this->assertFalse(rmdir($this->baz1URL));
  371. $this->assertFalse(rmdir($this->baz2URL));
  372. }
  373. /**
  374. * rmdir() can not remove a non-existing directory
  375. *
  376. * @test
  377. */
  378. public function rmdirCanNotRemoveNonExistingDirectory()
  379. {
  380. $this->assertFalse(rmdir($this->fooURL . '/another'));
  381. }
  382. /**
  383. * rmdir() can not remove non-empty directories
  384. *
  385. * @test
  386. */
  387. public function rmdirCanNotRemoveNonEmptyDirectory()
  388. {
  389. $this->assertFalse(rmdir($this->fooURL));
  390. $this->assertFalse(rmdir($this->barURL));
  391. }
  392. /**
  393. * @test
  394. */
  395. public function rmdirCanRemoveEmptyDirectory()
  396. {
  397. vfsStream::newDirectory('empty')->at($this->foo);
  398. $this->assertTrue($this->foo->hasChild('empty'));
  399. $this->assertTrue(rmdir($this->fooURL . '/empty'));
  400. $this->assertFalse($this->foo->hasChild('empty'));
  401. }
  402. /**
  403. * @test
  404. */
  405. public function rmdirCanRemoveEmptyDirectoryWithDot()
  406. {
  407. vfsStream::newDirectory('empty')->at($this->foo);
  408. $this->assertTrue($this->foo->hasChild('empty'));
  409. $this->assertTrue(rmdir($this->fooURL . '/empty/.'));
  410. $this->assertFalse($this->foo->hasChild('empty'));
  411. }
  412. /**
  413. * rmdir() can remove empty directories
  414. *
  415. * @test
  416. */
  417. public function rmdirCanRemoveEmptyRoot()
  418. {
  419. $this->foo->removeChild('bar');
  420. $this->foo->removeChild('baz2');
  421. $this->assertTrue(rmdir($this->fooURL));
  422. $this->assertFalse(file_exists($this->fooURL)); // make sure statcache was cleared
  423. $this->assertNull(vfsStreamWrapper::getRoot());
  424. }
  425. /**
  426. * @test
  427. * @group permissions
  428. * @group bug_15
  429. */
  430. public function rmdirDirCanNotRemoveDirFromNonWritingDirectory()
  431. {
  432. vfsStreamWrapper::register();
  433. vfsStreamWrapper::setRoot(new vfsStreamDirectory('root', 0000));
  434. vfsStreamWrapper::getRoot()->addChild(new vfsStreamDirectory('nonRemovableFolder'));
  435. $this->assertFalse(is_writable(vfsStream::url('root')));
  436. $this->assertFalse(rmdir(vfsStream::url('root/nonRemovableFolder')));
  437. $this->assertTrue(vfsStreamWrapper::getRoot()->hasChild('nonRemovableFolder'));
  438. }
  439. /**
  440. * @test
  441. * @group permissions
  442. * @group bug_17
  443. */
  444. public function issue17()
  445. {
  446. vfsStreamWrapper::register();
  447. vfsStreamWrapper::setRoot(new vfsStreamDirectory('root', 0770));
  448. vfsStreamWrapper::getRoot()->chgrp(vfsStream::GROUP_USER_1)
  449. ->chown(vfsStream::OWNER_USER_1);
  450. $this->assertFalse(mkdir(vfsStream::url('root/doesNotWork')));
  451. $this->assertFalse(vfsStreamWrapper::getRoot()->hasChild('doesNotWork'));
  452. }
  453. /**
  454. * @test
  455. * @group bug_19
  456. */
  457. public function accessWithDoubleDotReturnsCorrectContent()
  458. {
  459. $this->assertEquals('baz2',
  460. file_get_contents(vfsStream::url('foo/bar/../baz2'))
  461. );
  462. }
  463. /**
  464. * @test
  465. * @since 0.11.0
  466. * @group issue_23
  467. */
  468. public function unlinkCanNotRemoveNonEmptyDirectory()
  469. {
  470. try {
  471. $this->assertFalse(unlink($this->barURL));
  472. } catch (PHPUnit_Framework_Error $fe) {
  473. $this->assertEquals('unlink(vfs://foo/bar): Operation not permitted', $fe->getMessage());
  474. }
  475. $this->assertTrue($this->foo->hasChild('bar'));
  476. $this->assertFileExists($this->barURL);
  477. }
  478. /**
  479. * @test
  480. * @since 0.11.0
  481. * @group issue_23
  482. */
  483. public function unlinkCanNotRemoveEmptyDirectory()
  484. {
  485. vfsStream::newDirectory('empty')->at($this->foo);
  486. try {
  487. $this->assertTrue(unlink($this->fooURL . '/empty'));
  488. } catch (PHPUnit_Framework_Error $fe) {
  489. $this->assertEquals('unlink(vfs://foo/empty): Operation not permitted', $fe->getMessage());
  490. }
  491. $this->assertTrue($this->foo->hasChild('empty'));
  492. $this->assertFileExists($this->fooURL . '/empty');
  493. }
  494. }
  495. ?>