/tests/file/del.php

https://github.com/prophile/srobo-ide · PHP · 119 lines · 89 code · 20 blank · 10 comment · 4 complexity · 460843b3ee31de5dde01b2d261fab233 MD5 · raw file

  1. <?php
  2. //delete any existing repos
  3. if (is_dir("/tmp/test-repos"))
  4. {
  5. exec("rm -rf /tmp/test-repos");
  6. }
  7. exec("mkdir -p /tmp/test-repos");
  8. $config = Configuration::getInstance();
  9. $config->override("repopath", "/tmp/test-repos");
  10. $config->override("user.default", "bees");
  11. $config->override("user.default.teams", array(1, 2));
  12. $config->override("auth_module", "single");
  13. $config->override("modules", array("file", 'proj'));
  14. //do a quick authentication
  15. $auth = AuthBackend::getInstance();
  16. test_true($auth->authUser('bees','face'), "authentication failed");
  17. //setup the required input keys
  18. $input = Input::getInstance();
  19. $input->setInput("team", 1);
  20. $input->setInput("project", "monkies");
  21. $output = Output::getInstance();
  22. $mm = ModuleManager::getInstance();
  23. $mm->importModules();
  24. test_equal($mm->moduleExists("file"), true, "file module does not exist");
  25. $file = $mm->getModule('file');
  26. function getRepoPath()
  27. {
  28. $config = Configuration::getInstance();
  29. $input = Input::getInstance();
  30. return $config->getConfig("repopath") . "/" . $input->getInput("team") . "/users/bees/" . $input->getInput("project");
  31. }
  32. $repopath = getRepoPath();
  33. $projectManager = ProjectManager::getInstance();
  34. $projectManager->createRepository($input->getInput("team"), $input->getInput("project"));
  35. $repo = $projectManager->getUserRepository($input->getInput("team"), $input->getInput("project"), 'bees');
  36. test_true(is_dir($repopath), "created repo did not exist");
  37. function createAndAssertFileDeleted($path)
  38. {
  39. $config = Configuration::getInstance();
  40. $input = Input::getInstance();
  41. $output = Output::getInstance();
  42. echo "Testing deletion of file '$path' for team ", $input->getInput('team'), ', project ', $input->getInput('project'), ".\n";
  43. $repopath = getRepoPath();
  44. // need to do this each time since the modules are single-shot
  45. // that is, they only get one Input per instance, and may cache what it has to say.
  46. $mm = ModuleManager::getInstance();
  47. $mm->importModules();
  48. test_true($mm->moduleExists('file'), 'file module does not exist');
  49. $file = $mm->getModule('file');
  50. $proj = $mm->getModule('proj');
  51. test_nonnull($file, 'file module does not exist');
  52. test_nonnull($proj, 'proj module does not exist');
  53. // create the file
  54. $input->setInput('path', $path);
  55. $content = 'deathcakes'.$path;
  56. $input->setInput('data', $content);
  57. test_true($file->dispatchCommand('put'), "Failed to add content to the file '$path' to be removed.");
  58. $abspath = "$repopath/$path";
  59. test_true(file_exists($abspath), "failed to create file '$abspath'");
  60. // commit it
  61. $input->setInput('paths', array($path));
  62. $input->setInput('message', "Create '$path'.");
  63. test_true($proj->dispatchCommand('commit'), "Failed to commit file '$path' to be removed.");
  64. // delete the file
  65. $input->setInput('files', array($path));
  66. $file->dispatchCommand('del');
  67. $abspath = "$repopath/$path";
  68. test_false(file_exists($abspath), "failed to delete file '$abspath'");
  69. // commit
  70. $input->setInput('message', "Delete '$path'.");
  71. test_true($proj->dispatchCommand('commit'), "Failed to commit removal of file '$path'.");
  72. test_false(file_exists($abspath), "File '$abspath' exists after committing its removal.");
  73. // get the file-list to check that it's really gone
  74. $input->setInput('path', '.');
  75. test_true($file->dispatchCommand('list'), "Failed to get file list after removing '$path'.");
  76. $list = $output->getOutput('files');
  77. test_false(in_array($path, $list), "File '$abspath' listed after committing its removal.");
  78. test_false(file_exists($abspath), "File '$abspath' exists after getting file list after committing its removal.");
  79. }
  80. createAndAssertFileDeleted('simple-file-name');
  81. createAndAssertFileDeleted('spacey path');
  82. createAndAssertFileDeleted('subdir/file');
  83. createAndAssertFileDeleted('subdir/spacey path');
  84. createAndAssertFileDeleted('spacey subdir/spacey path');
  85. createAndAssertFileDeleted('variable $file name');
  86. $chars = '$%@~{}][()';
  87. for($i=0; $i < strlen($chars); $i++)
  88. {
  89. createAndAssertFileDeleted('char \''.$chars[$i].'\'.');
  90. }
  91. $unicodes = array('£', '❝', '♞');
  92. foreach($unicodes as $char)
  93. {
  94. createAndAssertFileDeleted('char \''.$char.'\'.');
  95. }
  96. if (is_dir("/tmp/test-repos"))
  97. {
  98. exec("rm -rf /tmp/test-repos");
  99. }