diff --git a/src/api/models/Category.php b/src/api/models/Category.php index 026d594..fbf7e39 100644 --- a/src/api/models/Category.php +++ b/src/api/models/Category.php @@ -1,39 +1,42 @@ loadFromBean($this->bean); } - public static function fromBean($container, $bean) { - $instance = new self($container, 0, true); - $instance->loadFromBean($bean); - - return $instance; - } - - public static function fromJson($container, $json) { - $instance = new self($container, 0, true); - $instance->loadFromJson($json); - - return $instance; - } - public function updateBean() { + $bean = $this->bean; + + $bean->id = $this->id; + $bean->name = $this->name; } public function loadFromBean($bean) { + if (!isset($bean->id) || $bean->id === 0) { + return; + } + + $this->loadPropertiesFrom($bean); } public function loadFromJson($json) { + $obj = json_decode($json); + + if (!isset($obj->id) || $obj->id === 0) { + return; + } + + $this->loadPropertiesFrom($obj); + } + + private function loadPropertiesFrom($obj) { + $this->id = (int) $obj->id; + $this->name = $obj->name; } } diff --git a/src/api/models/Column.php b/src/api/models/Column.php index 6af9246..bc828b5 100644 --- a/src/api/models/Column.php +++ b/src/api/models/Column.php @@ -5,37 +5,61 @@ class Column extends BaseModel { public $position = 0; public $tasks = []; // Task model array - public function __construct($container, $id = 0, $internal = false) { + public function __construct($container, $id = 0) { parent::__construct('column', $id, $container); - if ($internal) { - return; - } - $this->loadFromBean($this->bean); } - public static function fromBean($container, $bean) { - $instance = new self($container, 0, true); - $instance->loadFromBean($bean); - - return $instance; - } - - public static function fromJson($container, $json) { - $instance = new self($container, 0, true); - $instance->loadFromJson($json); - - return $instance; - } - public function updateBean() { + $bean = $this->bean; + + $bean->id = $this->id; + $bean->name = $this->name; + $bean->position = $this->position; + $bean->xownTaskList = []; + + foreach($this->tasks as $task) { + $bean->xownTaskList[] = $task->bean; + } } public function loadFromBean($bean) { + if (!isset($bean->id) || $bean->id === 0) { + return; + } + + $this->loadPropertiesFrom($bean); + $this->tasks = []; + + if (isset($bean->xownTaskList)) { + foreach($bean->xownTaskList as $item) { + $this->tasks[] = new Task($this->container, $item->id); + } + } } public function loadFromJson($json) { + $obj = json_decode($json); + + if (!isset($obj->id) || $obj->id === 0) { + return; + } + + $this->loadPropertiesFrom($obj); + $this->tasks = []; + + if (isset($obj->tasks)) { + foreach($obj->tasks as $item) { + $this->tasks[] = new Task($this->container, $item->id); + } + } + } + + private function loadPropertiesFrom($obj) { + $this->id = (int) $obj->id; + $this->name = $obj->name; + $this->position = (int) $obj->position; } } diff --git a/src/api/models/User.php b/src/api/models/User.php index ef0026b..0599f7f 100644 --- a/src/api/models/User.php +++ b/src/api/models/User.php @@ -17,37 +17,69 @@ class User extends BaseModel { public $default_board_id = 0; public $options = []; // UserOptions model - public function __construct($container, $id = 0, $internal = false) { + public function __construct($container, $id = 0) { parent::__construct('user', $id, $container); - if ($internal) { - return; - } - $this->loadFromBean($this->bean); } - public static function fromBean($container, $bean) { - $instance = new self($container, 0, true); - $instance->loadFromBean($bean); - - return $instance; - } - - public static function fromJson($container, $json) { - $instance = new self($container, 0, true); - $instance->loadFromJson($json); - - return $instance; - } - public function updateBean() { + $bean = $this->bean; + + $bean->id = $this->id; + $bean->security_level = $this->security_level; + $bean->username = $this->username; + $bean->salt = $this->salt; + $bean->password_hash = $this->password_hash; + $bean->email = $this->email; + $bean->default_board_id = $this->default_board_id; + $bean->xownOptionList = []; + + foreach($this->options as $option) { + $bean->xownOptionList[] = $option->bean; + } } public function loadFromBean($bean) { + if (!isset($bean->id) || $bean->id === 0) { + return; + } + + $this->loadPropertiesFrom($bean); + $this->options = []; + + if (isset($bean->xownOptionList)) { + foreach($bean->xownOptionList as $item) { + $this->options[] = new UserOptions($this->container, $item->id); + } + } } - public function loadFromJson($obj) { + public function loadFromJson($json) { + $obj = json_decode($json); + + if (!isset($obj->id) || $obj->id === 0) { + return; + } + + $this->loadPropertiesFrom($obj); + $this->options = []; + + if (isset($obj->options)) { + foreach($obj->options as $item) { + $this->options[] = new UserOptions($this->container, $item->id); + } + } + } + + public function loadPropertiesFrom($obj) { + $this->id = (int) $obj->id; + $this->security_level = new SecurityLevel((int) $obj->security_level); + $this->username = $obj->username; + $this->salt = $obj->salt; + $this->password_hash = $obj->password_hash; + $this->email = $obj->email; + $this->default_board_id = (int) $obj->default_board_id; } } diff --git a/src/api/models/UserOptions.php b/src/api/models/UserOptions.php index 490981c..e159128 100644 --- a/src/api/models/UserOptions.php +++ b/src/api/models/UserOptions.php @@ -1,8 +1,10 @@ bean; + + $bean->id = $this->id; + $bean->new_tasks_at_bottom = $this->new_tasks_at_bottom; + $bean->show_animations = $this->show_animations; + $bean->show_assignee = $this->show_assignee; + $bean->multiple_tasks_per_row = $this->multiple_tasks_per_row; } public function loadFromBean($bean) { + if (!isset($bean->id) || $bean->id === 0) { + return; + } + + $this->loadPropertiesFrom($bean); } public function loadFromJson($obj) { + $obj = json_decode($json); + + if (!isset($obj->id) || $obj->id === 0) { + return; + } + + $this->loadPropertiesFrom($obj); + } + + private function loadPropertiesFrom($obj) { + $this->id = (int) $obj->id; + $this->new_tasks_at_bottom = (bool) $obj->new_tasks_at_bottom; + $this->show_animations = (bool) $obj->show_animations; + $this->show_assignee = (bool) $obj->show_assignee; + $this->multiple_tasks_per_row = (bool) $obj->multiple_tasks_per_row; } } diff --git a/test/api/CategoryTest.php b/test/api/CategoryTest.php new file mode 100644 index 0000000..2332d16 --- /dev/null +++ b/test/api/CategoryTest.php @@ -0,0 +1,69 @@ +json !== '') { + return; + } + + $task = DataMock::getCategory(); + $this->json = json_encode($task); + $this->bean = $task; + } + + public function testCreateCategory() { + $category = new Category(new ContainerMock()); + $this->assertDefaultProperties($category); + } + + public function testLoadFromBean() { + $category = new Category(new ContainerMock()); + + $category->loadFromBean($this->bean); + $this->assertMockProperties($category); + } + + public function testLoadFromJson() { + $category = new Category(new ContainerMock()); + + $category->loadFromJson(''); + $this->assertDefaultProperties($category); + + $category->loadFromJson($this->json); + $this->assertMockProperties($category); + } + + public function testUpdateBean() { + $category = new Category(new ContainerMock()); + $category->loadFromBean($this->bean); + + $category->updateBean(); + $bean = $category->getBean(); + + $this->assertTrue($bean->id === $category->id); + $this->assertTrue($bean->name === $category->name); + } + + private function assertDefaultProperties($category) { + $this->assertTrue($category->id === 0); + $this->assertTrue($category->name === ''); + } + + private function assertMockProperties($category) { + $this->assertTrue($category->id === 1); + $this->assertTrue($category->name === 'cat1'); + } +} + diff --git a/test/api/ColumnTest.php b/test/api/ColumnTest.php new file mode 100644 index 0000000..6ff308e --- /dev/null +++ b/test/api/ColumnTest.php @@ -0,0 +1,74 @@ +json !== '') { + return; + } + + $column = DataMock::getColumn(); + $this->json = json_encode($column); + $this->bean = $column; + // Convert to bean format + $this->bean->xownTaskList = $column->tasks; + } + + public function testCreateColumn() { + $column = new Column(new ContainerMock()); + $this->assertDefaultProperties($column); + } + + public function testLoadFromBean() { + $column = new Column(new ContainerMock()); + + $column->loadFromBean($this->bean); + $this->assertMockProperties($column); + } + + public function testLoadFromJson() { + $column = new Column(new ContainerMock()); + + $column->loadFromJson(''); + $this->assertDefaultProperties($column); + + $column->loadFromJson($this->json); + $this->assertMockProperties($column); + } + + public function testUpdateBean() { + $column = new Column(new ContainerMock()); + $column->loadFromBean($this->bean); + + $column->updateBean(); + $bean = $column->getBean(); + + $this->assertTrue($bean->id === $column->id); + $this->assertTrue($bean->name === $column->name); + $this->assertTrue($bean->position === $column->position); + } + + private function assertDefaultProperties($column) { + $this->assertTrue($column->id === 0); + $this->assertTrue($column->name === ''); + $this->assertTrue($column->position === 0); + } + + private function assertMockProperties($column) { + $this->assertTrue($column->id === 1); + $this->assertTrue($column->name === 'col1'); + $this->assertTrue($column->position === 1); + } +} + diff --git a/test/api/Mocks.php b/test/api/Mocks.php index f2e7cb0..4187de9 100644 --- a/test/api/Mocks.php +++ b/test/api/Mocks.php @@ -28,6 +28,8 @@ class DataMock { $column = new stdClass(); $column->id = 1; $column->name = 'col1'; + $column->position = 1; + $column->tasks[] = DataMock::getTask(); return $column; } @@ -54,8 +56,13 @@ class DataMock { public static function getUser() { $user = new stdClass(); $user->id = 1; - $user->security_level = 1; + $user->security_level = SecurityLevel::BoardAdmin; $user->username = 'tester'; + $user->salt = 'salty1234'; + $user->password_hash = 'hashpass1234'; + $user->email = 'user@example.com'; + $user->default_board_id = 1; + $user->options[] = DataMock::getUserOptions(); return $user; } @@ -110,6 +117,17 @@ class DataMock { return $task; } + + public static function getUserOptions() { + $options = new stdClass(); + $options->id = 1; + $options->new_tasks_at_bottom = false; + $options->show_animations = false; + $options->show_assignee = false; + $options->multiple_tasks_per_row = true; + + return $options; + } } class LoggerMock { diff --git a/test/api/UserOptionsTest.php b/test/api/UserOptionsTest.php new file mode 100644 index 0000000..108bb30 --- /dev/null +++ b/test/api/UserOptionsTest.php @@ -0,0 +1,78 @@ +json !== '') { + return; + } + + $options = DataMock::getUserOptions(); + $this->json = json_encode($options); + $this->bean = $user; + } + + public function testCreateUserOptions() { + $options = new UserOptions(new ContainerMock()); + $this->assertDefaultProperties($options); + } + + public function testLoadFromBean() { + $options = new UserOptions(new ContainerMock()); + + $options->loadFromBean($this->bean); + $this->assertMockProperties($options); + } + + public function testLoadFromJson() { + $options = new UserOptions(new ContainerMock()); + + $options->loadFromJson(''); + $this->assertDefaultProperties($options); + + $options->loadFromJson($this->json); + $this->assertMockProperties($options); + } + + public function testUpdateBean() { + $options = new UserOptions(new ContainerMock()); + $options->loadFromBean($this->bean); + + $options->updateBean(); + $bean = $options->getBean(); + + $this->assertTrue($bean->id === $user->id); + $this->assertTrue($bean->new_tasks_at_bottom === $user->new_tasks_at_bottom); + $this->assertTrue($bean->show_animations === $user->show_animations); + $this->assertTrue($bean->show_assignee === $user->show_assignee); + $this->assertTrue($bean->multiple_tasks_per_row === $user->multiple_tasks_per_row); + } + + private function assertDefaultProperties($user) { + $this->assertTrue($user->id === 0); + $this->assertTrue($user->security_level == SecurityLevel::User); + $this->assertTrue($user->username === ''); + $this->assertTrue($user->salt === ''); + $this->assertTrue($user->password_hash === ''); + } + + private function assertMockProperties($user) { + $this->assertTrue($user->id === 1); + $this->assertTrue($user->salt === 'salty1234'); + $this->assertTrue($user->password_hash === 'hashpass1234'); + $this->assertTrue($user->email === 'user@example.com'); + $this->assertTrue($user->default_board_id === 1); + } +} + diff --git a/test/api/UserTest.php b/test/api/UserTest.php new file mode 100644 index 0000000..b9b96e8 --- /dev/null +++ b/test/api/UserTest.php @@ -0,0 +1,87 @@ +json !== '') { + return; + } + + $user = DataMock::getUser(); + $this->json = json_encode($user); + $this->bean = $user; + // Convert to bean format + $this->bean->xownOptionList = $user->options; + } + + public function testCreateUser() { + $user = new User(new ContainerMock()); + $this->assertDefaultProperties($user); + } + + public function testLoadFromBean() { + $user = new User(new ContainerMock()); + + $user->loadFromBean($this->bean); + $this->assertMockProperties($user); + } + + public function testLoadFromJson() { + $user = new User(new ContainerMock()); + + $user->loadFromJson(''); + $this->assertDefaultProperties($user); + + $user->loadFromJson($this->json); + $this->assertMockProperties($user); + } + + public function testUpdateBean() { + $user = new User(new ContainerMock()); + $user->loadFromBean($this->bean); + + $user->updateBean(); + $bean = $user->getBean(); + + $this->assertTrue($bean->id === $user->id); + $this->assertTrue($bean->security_level === $user->security_level); + $this->assertTrue($bean->username === $user->username); + $this->assertTrue($bean->salt === $user->salt); + $this->assertTrue($bean->password_hash === $user->password_hash); + $this->assertTrue($bean->email === $user->email); + $this->assertTrue($bean->default_board_id === $user->default_board_id); + } + + private function assertDefaultProperties($user) { + $this->assertTrue($user->id === 0); + $this->assertTrue($user->security_level == SecurityLevel::User); + $this->assertTrue($user->username === ''); + $this->assertTrue($user->salt === ''); + $this->assertTrue($user->password_hash === ''); + $this->assertTrue($user->email === ''); + $this->assertTrue($user->default_board_id === 0); + } + + private function assertMockProperties($user) { + $this->assertTrue($user->id === 1); + $this->assertTrue($user->security_level->getValue() === + SecurityLevel::BoardAdmin); + $this->assertTrue($user->username === 'tester'); + $this->assertTrue($user->salt === 'salty1234'); + $this->assertTrue($user->password_hash === 'hashpass1234'); + $this->assertTrue($user->email === 'user@example.com'); + $this->assertTrue($user->default_board_id === 1); + } +} + diff --git a/tests.db b/tests.db index 38058c1..0f6e3e8 100644 Binary files a/tests.db and b/tests.db differ