forked from DistributedProofreaders/dproofreaders
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a generic JSON-based storage class and an extension of it that will be used in the API to store blobs from clients. This is a similar structure to the Settings class / user_settings table but enforces a JSON value. This creates a shortcut in the API to allow JSON input to bypass being deserialized and the output to bypass being serialized and allow the routing function to manage that.
- Loading branch information
Showing
17 changed files
with
440 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
class StorageTest extends PHPUnit\Framework\TestCase | ||
{ | ||
//------------------------------------------------------------------------ | ||
// Basic JSON storage test | ||
|
||
public function test_valid_json(): void | ||
{ | ||
$storage = new JsonStorage("username"); | ||
$storage->set("setting", "{}"); | ||
$value = $storage->get("setting"); | ||
$this->assertEquals("{}", $value); | ||
$value = $storage->delete("setting"); | ||
$this->assertEquals(null, $value); | ||
} | ||
|
||
public function test_invalid_json(): void | ||
{ | ||
$this->expectException(ValueError::class); | ||
$storage = new JsonStorage("username"); | ||
$storage->set("setting", "blearg"); | ||
} | ||
|
||
//------------------------------------------------------------------------ | ||
// API storage test | ||
|
||
public function test_valid_storagekey(): void | ||
{ | ||
global $api_storage_keys; | ||
$api_storage_keys = ["valid"]; | ||
|
||
$storage = new ApiStorage("valid", "username"); | ||
$storage->set("{}"); | ||
$value = $storage->get(); | ||
$this->assertEquals("{}", $value); | ||
$value = $storage->delete(); | ||
$this->assertEquals(null, $value); | ||
} | ||
|
||
public function test_invalid_storagekey(): void | ||
{ | ||
global $api_storage_keys; | ||
$api_storage_keys = []; | ||
|
||
$this->expectException(ValueError::class); | ||
|
||
$storage = new ApiStorage("invalid", "username"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
$relPath = '../../../pinc/'; | ||
include_once($relPath.'base.inc'); | ||
|
||
// ------------------------------------------------------------ | ||
|
||
echo "Creating json_storage table...\n"; | ||
|
||
$sql = " | ||
CREATE TABLE json_storage ( | ||
username varchar(25) NOT NULL, | ||
setting varchar(32) NOT NULL, | ||
value json NOT NULL, | ||
timestamp int NOT NULL default 0, | ||
PRIMARY KEY (username, setting) | ||
) | ||
"; | ||
|
||
mysqli_query(DPDatabase::get_connection(), $sql) or die(mysqli_error(DPDatabase::get_connection())); | ||
|
||
// ------------------------------------------------------------ | ||
|
||
echo "\nDone!\n"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.