Browse files

Merge branch 'master' of github.com:thibaud-rohmer/PhotoShow

  • Loading branch information...
2 parents c9275f8 + 73e664c commit b7fd60619c34956906788e4295620fb0297f299a @thibaud-rohmer committed Apr 20, 2012
Showing with 205 additions and 6 deletions.
  1. +16 −6 src/classes/GuestToken.php
  2. +151 −0 src/tests/GuestTokenTest.php
  3. +38 −0 src/tests/TestUnit.php
View
22 src/classes/GuestToken.php
@@ -131,7 +131,7 @@ public function save(){
* @author Franck Royer
*/
public static function delete($key){
- if (!CurrentUser::$admin){
+ if (!CurrentUser::$admin || !file_exists(CurrentUser::$tokens_file)){
// Only admin can delete the tokens for now
return false;
}
@@ -143,12 +143,17 @@ public static function delete($key){
foreach( $xml as $tk ){
if((string)$tk->key == $key){
unset($xml->token[$i]);
+ $found = true;
break;
}
$i++;
}
- $xml->asXML(CurrentUser::$tokens_file);
+ if ($found && $xml->asXML(CurrentUser::$tokens_file)){
+ return true;
+ } else {
+ return false;
+ }
}
/**
@@ -195,8 +200,8 @@ public static function findAll(){
foreach( $xml as $token ){
$new_token=array();
- $new_token['key'] = $token->key;
- $new_token['path'] = $token->path;
+ $new_token['key'] = (string)$token->key;
+ $new_token['path'] = (string)$token->path;
$tokens[]=$new_token;
}
@@ -292,8 +297,13 @@ public static function get_url($key){
* @author Franck Royer
*/
public static function view($key,$path){
- $rpath = File::a2r($path);
- $apath = self::get_path($key);
+ $rpath = File::a2r($path)."/";
+ $apath = self::get_path($key)."/";
+
+ // Remove double slashes
+ preg_replace('/\/\/+/','/', $rpath);
+ preg_replace('/\/\/+/','/', $apath);
+
// Check if the tokens file exists
if(!file_exists(CurrentUser::$tokens_file)){
View
151 src/tests/GuestTokenTest.php
@@ -69,8 +69,159 @@ public function test_generate_key_random()
}
/**
+ * Test the create feature
+ * @test
+ * @depends test_generate_key
+ */
+ public function test_create(){
+ // From scratch
+ self::delete_tokens_file();
+ self::login_as_admin();
+
+ $folder1 = Settings::$photos_dir."tokenfolder";
+ $ret = GuestToken::create($folder1);
+ $this->assertTrue($ret);
+
+ $tokens = GuestToken::findAll();
+ $this->assertCount(1, $tokens);
+ $this->assertArrayHasKey('key', $tokens[0]);
+ $this->assertArrayHasKey('path', $tokens[0]);
+ $this->assertEquals(File::a2r($folder1), $tokens[0]['path']);
+ $this->assertRegexp('/.{10}.*/',$tokens[0]['key']);
+
+ // we shouldn't create key for non-existing folders
+ try {
+ $folder2 = Settings::$photos_dir."tokenfolder2";
+ if (file_exists($folder2)){
+ rmdir($folder2);
+ }
+ $ret = GuestToken::create($folder2);
+ } catch(Exception $e) {
+ $this->assertCount(1, GuestToken::findAll());
+
+ mkdir($folder2);
+ $ret = GuestToken::create($folder2);
+ $this->assertTrue($ret);
+ $this->assertCount(2, GuestToken::findAll());
+
+ $this->assertCount(1, GuestToken::find_for_path($folder2));
+
+ $tokens2 = GuestToken::find_for_path($folder2);
+
+ $this->assertEquals(File::a2r($folder2), $tokens2[0]['path']);
+
+ $ret = GuestToken::exist($tokens[0]['key']);
+ $this->assertTrue($ret);
+
+ $ret = GuestToken::delete($tokens[0]['key']);
+ $this->assertTrue($ret);
+ $this->assertCount(1, GuestToken::findAll());
+
+ return;
+ }
+ $this->fail('Token has been creating on an inexisting folder');
+ }
+
+ /**
+ * test exists function
+ * @test
+ * @depends test_create
+ */
+ public function test_exist(){
+ self::login_as_admin();
+ self::create_token();
+
+ $tokens = GuestToken::findAll();
+ $key = $tokens[0]['key'];
+
+ $this->assertTrue(GuestToken::exist($key));
+ }
+
+
+ /**
+ * test get_path function
+ * @test
+ * @depends test_create
+ * @depends test_generate_key
+ */
+ public function test_get_path(){
+ self::login_as_admin();
+ $key = Guesttoken::generate_key();
+ $path = Settings::$photos_dir."/tokenfolder";
+
+ GuestToken::create($path,$key);
+
+ $tpath = GuestToken::get_path($key);
+ $this->assertEquals(File::a2r($path), $tpath);
+
+ }
+
+ /**
+ * test view function
+ * @test
+ * @depends test_generate_key
+ */
+ public function test_view(){
+ //prepare
+ self::login_as_admin();
+ self::delete_tokens_file();
+ $paths = array();
+ $keys = array();
+ $paths[1] = Settings::$photos_dir."/tokenfolder";
+ $paths[2] = Settings::$photos_dir."/tokenfolder2";
+ $paths[3] = Settings::$photos_dir."/tokenfolder/subfolder";
+
+ for ($i = 1; $i <= 3; $i++){
+ if (!file_exists($paths[$i])){
+ mkdir($paths[$i]);
+ }
+
+ $keys[$i] = Guesttoken::generate_key();
+ GuestToken::create($paths[$i],$keys[$i]);
+ }
+ CurrentUser::logout();
+
+ //test
+ for ($i = 1; $i <= 3; $i++){
+ $this->assertTrue(GuestToken::view($keys[$i], $paths[$i]));
+ }
+
+ $this->assertFalse(GuestToken::view($keys[3], $paths[1]));
+ $this->assertFalse(GuestToken::view($keys[3], $paths[2]));
+ $this->assertFalse(GuestToken::view($keys[2], $paths[1]));
+ $this->assertTrue(GuestToken::view($keys[1], $paths[3]));
+
+ }
+
+ /**
+ * test delete
+ * @test
+ * @depends test_create
+ * @depends test_generate_key
+ * @depends test_exist
+ */
+ public function test_delete(){
+ self::login_as_admin();
+ self::delete_tokens_file();
+ $key = Guesttoken::generate_key();
+ $path = Settings::$photos_dir."/tokenfolder";
+ $key2 = Guesttoken::generate_key();
+ $path2 = Settings::$photos_dir."/subfolder";
+ GuestToken::create($path,$key);
+ GuestToken::create($path2,$key2);
+
+ $this->assertFalse(GuestToken::delete(GuestToken::generate_key()));
+ $this->assertCount(2, GuestToken::findAll());
+ $this->assertTrue(GuestToken::delete($key));
+ $this->assertCount(1, GuestToken::findAll());
+ $this->assertFalse(GuestToken::exist($key));
+ $this->assertTrue(GuestToken::exist($key2));
+ }
+
+ /**
* Verify toHTML gives an output
* @test
+ * @depends test_create
*/
public function test_toHTML()
{
View
38 src/tests/TestUnit.php
@@ -78,6 +78,11 @@ function __construct()
require_once('PHPUnit'.DIRECTORY_SEPARATOR.'Framework'.DIRECTORY_SEPARATOR.'TestCase.php');
parent::__construct();
}
+ function __destruct()
+ {
+ self::clean_files();
+ }
+
/**
* includes all PhotoShow classes
@@ -165,6 +170,39 @@ public static function create_accounts(){
}
}
+ /**
+ * Function to clean the files
+ */
+ public static function clean_files(){
+ if (!include($GLOBALS['config_file'])){
+ throw new Exception("Cannot include config file!\n");
+ }
+
+ $parent_folder= dirname($config->photos_dir);
+ if (!file_exists($parent_folder)){
+ return;
+ }
+
+ if (!preg_match('/.*tmp\/?$/', $parent_folder)){
+ echo ("Folder is not named tmp, I am not taking the risk to delete it");
+ return;
+ }
+ self::rrmdir($parent_folder);
+ }
+
+ /**
+ * Recursively delete a folder
+ */
+ protected function rrmdir($dir) {
+ foreach(glob($dir . '/*') as $file) {
+ if(is_dir($file))
+ self::rrmdir($file);
+ else
+ unlink($file);
+ }
+ rmdir($dir);
+ }
+
/*****************************************************/
/* Functions used to setup scenarios for other tests */
/* */

0 comments on commit b7fd606

Please sign in to comment.