aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--engine/lib/filestore.php67
-rwxr-xr-xengine/tests/elgg_unit_test.php16
-rw-r--r--engine/tests/objects/filestore.php106
-rw-r--r--engine/tests/objects/users.php10
4 files changed, 193 insertions, 6 deletions
diff --git a/engine/lib/filestore.php b/engine/lib/filestore.php
index 852fbae8f..92e8a760e 100644
--- a/engine/lib/filestore.php
+++ b/engine/lib/filestore.php
@@ -250,7 +250,7 @@ class ElggDiskFilestore extends ElggFilestore {
throw new InvalidParameterException(sprintf(elgg_echo('InvalidParameterException:MissingOwner'), $file->getFilename(), $file->guid));
}
- return $this->dir_root . $this->make_file_matrix($owner->username) . $file->getFilename();
+ return $this->dir_root . $this->make_file_matrix($owner->guid) . $file->getFilename();
}
public function grabFile(ElggFile $file) {
@@ -262,8 +262,8 @@ class ElggDiskFilestore extends ElggFilestore {
}
public function getSize($prefix,$container_guid) {
- if ($container_guid && ($container=get_entity($container_guid)) && ($username = $container->username)) {
- return get_dir_size($this->dir_root.$this->make_file_matrix($username).$prefix);
+ if ($container_guid) {
+ return get_dir_size($this->dir_root.$this->make_file_matrix($container_guid).$prefix);
} else {
return false;
}
@@ -317,9 +317,56 @@ class ElggDiskFilestore extends ElggFilestore {
/**
* Construct the filename matrix.
*
- * @param string $filename
+ * @param int | string $identifier
+ * @return str
*/
- protected function make_file_matrix($filename) {
+ protected function make_file_matrix($identifier) {
+ if (is_numeric($identifier)) {
+ return $this->user_file_matrix($identifier);
+ }
+
+ return $this->deprecated_file_matrix($identifier);
+ }
+
+ /**
+ * Construct the filename matrix with user info
+ *
+ * This method will generate a matrix using the entity's creation time and
+ * unique guid. This is intended only to determine a user's data directory.
+ *
+ * @param int $guid
+ * @return str
+ */
+ protected function user_file_matrix($guid) {
+ // lookup the entity
+ $user = get_entity($guid);
+ if ($user->type != 'user')
+ {
+ // only to be used for user directories
+ return FALSE;
+ }
+
+ if (!$user->time_created) {
+ // fall back to deprecated method
+ return $this->deprecated_file_matrix($user->username);
+ }
+
+ $time_created = date('Y/m/d', $user->time_created);
+ return "$time_created/$user->guid/";
+ }
+
+ /**
+ * Construct the filename matrix using a string
+ *
+ * Particularly, this is used with a username to generate the file storage
+ * location.
+ *
+ * @deprecated for user directories: use user_file_matrix() instead.
+ *
+ * @param str $filename
+ * @return str
+ */
+ protected function deprecated_file_matrix($filename) {
$invalid_fs_chars = '*\'\\/"!$%^&*.%(){}[]#~?<>;|¬`@-+=';
$matrix = "";
@@ -1316,4 +1363,12 @@ function filestore_init() {
}
// Register a startup event
-register_elgg_event_handler('init', 'system', 'filestore_init', 100); \ No newline at end of file
+register_elgg_event_handler('init', 'system', 'filestore_init', 100);
+
+// Unit testing
+register_plugin_hook('unit_test', 'system', 'filestore_test');
+function filestore_test($hook, $type, $value, $params) {
+ global $CONFIG;
+ $value[] = "{$CONFIG->path}engine/tests/objects/filestore.php";
+ return $value;
+}
diff --git a/engine/tests/elgg_unit_test.php b/engine/tests/elgg_unit_test.php
index d9841099b..70f8788a0 100755
--- a/engine/tests/elgg_unit_test.php
+++ b/engine/tests/elgg_unit_test.php
@@ -1,12 +1,28 @@
<?php
+/**
+ * Elgg Core Unit Tester
+ *
+ * This class is to be extended by all Elgg unit tests. As such, any method here
+ * will be available to the tests.
+ */
abstract class ElggCoreUnitTest extends UnitTestCase
{
+ /**
+ * Class constructor.
+ *
+ * A simple wrapper to call the parent constructor.
+ */
public function __construct()
{
parent::__construct();
}
+ /**
+ * Class destructor.
+ *
+ * The parent does not provide a destructor, so including an explicit one here.
+ */
public function __destruct()
{
}
diff --git a/engine/tests/objects/filestore.php b/engine/tests/objects/filestore.php
new file mode 100644
index 000000000..76583a463
--- /dev/null
+++ b/engine/tests/objects/filestore.php
@@ -0,0 +1,106 @@
+<?php
+/**
+ * Elgg Test Skeleton
+ *
+ * @package Elgg
+ * @subpackage Test
+ * @author Curverider Ltd
+ * @link http://elgg.org/
+ */
+class ElggCoreFilestoreTest extends ElggCoreUnitTest {
+
+ /**
+ * Called before each test object.
+ */
+ public function __construct() {
+ parent::__construct();
+
+ // all code should come after here
+ }
+
+ /**
+ * Called before each test method.
+ */
+ public function setUp() {
+ $this->filestore = new ElggDiskFilestoreTest();
+ }
+
+ /**
+ * Called after each test method.
+ */
+ public function tearDown() {
+ // do not allow SimpleTest to interpret Elgg notices as exceptions
+ $this->swallowErrors();
+
+ unset($this->filestore);
+ }
+
+ /**
+ * Called after each test object.
+ */
+ public function __destruct() {
+ // all code should go above here
+ parent::__destruct();
+ }
+
+ public function testFileMatrix() {
+ global $CONFIG;
+
+ // create a test user
+ $user = $this->createTestUser();
+ $created = date('Y/m/d', $user->time_created);
+
+ // check matrix with username
+ $user_dir = $this->filestore->make_file_matrix($user->username);
+ $this->assertIdentical($user_dir, "f/i/l/e/T/fileTest/");
+
+ // check matrix with guid
+ $guid_dir = $this->filestore->make_file_matrix($user->guid);
+ $this->assertIdentical($guid_dir, "$created/$user->guid/");
+ $this->dump($user_dir);$this->dump($guid_dir);
+
+ // clean up user
+ $user->delete();
+ }
+
+ public function testFilenameOnFilestore() {
+ global $CONFIG;
+
+ // create a user to own the file
+ $user = $this->createTestUser();
+ $created = date('Y/m/d', $user->time_created);
+
+ // setup a test file; no need to save
+ $file = new ElggFile();
+ $file->owner_guid = $user->guid;
+ $file->setFilename('testing/filestore.txt');
+
+ // ensure filename and path is expected
+ $filename = $file->getFilenameOnFilestore($file);
+ $filepath = "$CONFIG->dataroot$created/$user->guid/testing/filestore.txt";
+ $this->assertIdentical($filename, $filepath);
+
+ // clean up user
+ $user->delete();
+ }
+
+
+ protected function createTestUser($username = 'fileTest') {
+ $user = new ElggUser();
+ $user->username = $username;
+ $guid = $user->save();
+
+ // load user to have access to creation time
+ return get_entity($guid);
+ }
+}
+
+class ElggDiskFilestoreTest extends ElggDiskFilestore {
+ public function make_file_matrix($filename) {
+ return parent::make_file_matrix($filename);
+ }
+
+ public function user_file_matrix($guid) {
+ return parent::user_file_matrix($guid);
+ }
+}
diff --git a/engine/tests/objects/users.php b/engine/tests/objects/users.php
index 5c88e4ece..358a80f1c 100644
--- a/engine/tests/objects/users.php
+++ b/engine/tests/objects/users.php
@@ -142,6 +142,16 @@ class ElggCoreUserTest extends ElggCoreUnitTest {
$object->delete();
}
+ public function testElggUserSave() {
+ // new object
+ $this->AssertEqual($this->user->getGUID(), 0);
+ $guid = $this->user->save();
+ $this->AssertNotEqual($guid, 0);
+
+ // clean up
+ $this->user->delete();
+ }
+
protected function fetchUser($guid) {
global $CONFIG;