mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-01-11 17:58:41 +00:00
Bug fixes and unit tests implemented
This commit is contained in:
parent
427c97bfb3
commit
52d8b17914
|
|
@ -421,12 +421,12 @@ class DataMapperAbstract implements DataMapperInterface
|
|||
$id = self::createOwnsOne($propertyName, $property->getValue($obj));
|
||||
$value = self::parseValue($column['type'], $id);
|
||||
|
||||
$query->insert($column['name'])->value($value, $column['type']);
|
||||
$query->insert($column['name'])->value($value);
|
||||
} elseif (isset(static::$belongsTo[$propertyName])) {
|
||||
$id = self::createBelongsTo($propertyName, $property->getValue($obj));
|
||||
$value = self::parseValue($column['type'], $id);
|
||||
|
||||
$query->insert($column['name'])->value($value, $column['type']);
|
||||
$query->insert($column['name'])->value($value);
|
||||
} elseif ($column['name'] !== static::$primaryField) {
|
||||
$tValue = $property->getValue($obj);
|
||||
if (\stripos($column['internal'], '/') !== false) {
|
||||
|
|
@ -439,7 +439,7 @@ class DataMapperAbstract implements DataMapperInterface
|
|||
|
||||
$value = self::parseValue($column['type'], $tValue);
|
||||
|
||||
$query->insert($column['name'])->value($value, $column['type']);
|
||||
$query->insert($column['name'])->value($value);
|
||||
}
|
||||
|
||||
if (!$isPublic) {
|
||||
|
|
@ -449,7 +449,7 @@ class DataMapperAbstract implements DataMapperInterface
|
|||
|
||||
// if a table only has a single column = primary key column. This must be done otherwise the query is empty
|
||||
if ($query->getType() === QueryType::NONE) {
|
||||
$query->insert(static::$primaryField)->value(0, static::$columns[static::$primaryField]['type']);
|
||||
$query->insert(static::$primaryField)->value(0);
|
||||
}
|
||||
|
||||
self::$db->con->prepare($query->toSql())->execute();
|
||||
|
|
@ -490,22 +490,22 @@ class DataMapperAbstract implements DataMapperInterface
|
|||
$id = self::createOwnsOneArray($column['internal'], $property);
|
||||
$value = self::parseValue($column['type'], $id);
|
||||
|
||||
$query->insert($column['name'])->value($value, $column['type']);
|
||||
$query->insert($column['name'])->value($value);
|
||||
} elseif (isset(static::$belongsTo[$path])) {
|
||||
$id = self::createBelongsToArray($column['internal'], $property);
|
||||
$value = self::parseValue($column['type'], $id);
|
||||
|
||||
$query->insert($column['name'])->value($value, $column['type']);
|
||||
$query->insert($column['name'])->value($value);
|
||||
} elseif ($column['internal'] === $path && $column['name'] !== static::$primaryField) {
|
||||
$value = self::parseValue($column['type'], $property);
|
||||
|
||||
$query->insert($column['name'])->value($value, $column['type']);
|
||||
$query->insert($column['name'])->value($value);
|
||||
}
|
||||
}
|
||||
|
||||
// if a table only has a single column = primary key column. This must be done otherwise the query is empty
|
||||
if ($query->getType() === QueryType::NONE) {
|
||||
$query->insert(static::$primaryField)->value(0, static::$columns[static::$primaryField]['type']);
|
||||
$query->insert(static::$primaryField)->value(0);
|
||||
}
|
||||
|
||||
self::$db->con->prepare($query->toSql())->execute();
|
||||
|
|
@ -1272,13 +1272,13 @@ class DataMapperAbstract implements DataMapperInterface
|
|||
$value = self::parseValue($column['type'], $id);
|
||||
|
||||
// todo: should not be done if the id didn't change. but for now don't know if id changed
|
||||
$query->set([static::$table . '.' . $column['name'] => $value], $column['type']);
|
||||
$query->set([static::$table . '.' . $column['name'] => $value]);
|
||||
} elseif (isset(static::$belongsTo[$propertyName])) {
|
||||
$id = self::updateBelongsTo($propertyName, $property->getValue($obj), $relations, $depth);
|
||||
$value = self::parseValue($column['type'], $id);
|
||||
|
||||
// todo: should not be done if the id didn't change. but for now don't know if id changed
|
||||
$query->set([static::$table . '.' . $column['name'] => $value], $column['type']);
|
||||
$query->set([static::$table . '.' . $column['name'] => $value]);
|
||||
} elseif ($column['name'] !== static::$primaryField) {
|
||||
$tValue = $property->getValue($obj);
|
||||
if (\stripos($column['internal'], '/') !== false) {
|
||||
|
|
@ -1290,7 +1290,7 @@ class DataMapperAbstract implements DataMapperInterface
|
|||
}
|
||||
$value = self::parseValue($column['type'], $tValue);
|
||||
|
||||
$query->set([static::$table . '.' . $column['name'] => $value], $column['type']);
|
||||
$query->set([static::$table . '.' . $column['name'] => $value]);
|
||||
}
|
||||
|
||||
if (!$isPublic) {
|
||||
|
|
@ -1340,17 +1340,17 @@ class DataMapperAbstract implements DataMapperInterface
|
|||
$value = self::parseValue($column['type'], $id);
|
||||
|
||||
// todo: should not be done if the id didn't change. but for now don't know if id changed
|
||||
$query->set([static::$table . '.' . $column['name'] => $value], $column['type']);
|
||||
$query->set([static::$table . '.' . $column['name'] => $value]);
|
||||
} elseif (isset(static::$belongsTo[$path])) {
|
||||
$id = self::updateBelongsToArray($column['internal'], $property, $relations, $depth);
|
||||
$value = self::parseValue($column['type'], $id);
|
||||
|
||||
// todo: should not be done if the id didn't change. but for now don't know if id changed
|
||||
$query->set([static::$table . '.' . $column['name'] => $value], $column['type']);
|
||||
$query->set([static::$table . '.' . $column['name'] => $value]);
|
||||
} elseif ($column['name'] !== static::$primaryField) {
|
||||
$value = self::parseValue($column['type'], $property);
|
||||
|
||||
$query->set([static::$table . '.' . $column['name'] => $value], $column['type']);
|
||||
$query->set([static::$table . '.' . $column['name'] => $value]);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -943,17 +943,22 @@ final class Builder extends BuilderAbstract
|
|||
* Values to insert.
|
||||
*
|
||||
* @param mixed $value Values
|
||||
* @param string $type Data type to insert
|
||||
*
|
||||
* @return Builder
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function value($value, string $type = 'string') : Builder
|
||||
public function value($value) : Builder
|
||||
{
|
||||
\end($this->values);
|
||||
$key = \key($this->values);
|
||||
$this->values[$key][] = $value;
|
||||
$key = \key($this->values);
|
||||
|
||||
if (\is_array($value)) {
|
||||
$this->values[$key] = $value;
|
||||
} else {
|
||||
$this->values[$key][] = $value;
|
||||
}
|
||||
|
||||
\reset($this->values);
|
||||
|
||||
return $this;
|
||||
|
|
@ -970,7 +975,7 @@ final class Builder extends BuilderAbstract
|
|||
*/
|
||||
public function sets(...$sets) : Builder
|
||||
{
|
||||
$this->sets[] = $sets;
|
||||
$this->sets[$sets[0]] = $sets[1] ?? null;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
|
@ -979,15 +984,14 @@ final class Builder extends BuilderAbstract
|
|||
* Values to insert.
|
||||
*
|
||||
* @param mixed $set Values
|
||||
* @param string $type Data type to insert
|
||||
*
|
||||
* @return Builder
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function set($set, string $type = 'string') : Builder
|
||||
public function set($set) : Builder
|
||||
{
|
||||
$this->sets[key($set)] = \current($set);
|
||||
$this->sets[\key($set)] = \current($set);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -240,7 +240,7 @@ class Meta implements RenderableInterface
|
|||
*/
|
||||
public function render() : string
|
||||
{
|
||||
return (\count($this->keywords) > 0 ? '<meta name="keywords" content="' . ViewAbstract::html(\implode(',', $this->keywords)) . '">"' : '')
|
||||
return (\count($this->keywords) > 0 ? '<meta name="keywords" content="' . ViewAbstract::html(\implode(',', $this->keywords)) . '">' : '')
|
||||
. (!empty($this->author) ? '<meta name="author" content="' . ViewAbstract::html($this->author) . '">' : '')
|
||||
. (!empty($this->description) ? '<meta name="description" content="' . ViewAbstract::html($this->description) . '">' : '')
|
||||
. (!empty($this->charset) ? '<meta charset="' . ViewAbstract::html($this->charset) . '">' : '')
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ declare(strict_types=1);
|
|||
|
||||
namespace phpOMS\Module;
|
||||
|
||||
use phpOMS\DataStorage\Database\Query\Builder;
|
||||
use phpOMS\DataStorage\Database\DatabaseType;
|
||||
use phpOMS\DataStorage\Database\Exception\InvalidDatabaseTypeException;
|
||||
use phpOMS\DataStorage\Database\DatabasePool;
|
||||
|
|
@ -45,43 +46,33 @@ class InstallerAbstract
|
|||
*/
|
||||
public static function registerInDatabase(DatabasePool $dbPool, InfoManager $info) : void
|
||||
{
|
||||
switch ($dbPool->get()->getType()) {
|
||||
case DatabaseType::MYSQL:
|
||||
$dbPool->get()->con->beginTransaction();
|
||||
$queryModule = new Builder($dbPool->get('insert'));
|
||||
$queryModule->prefix($dbPool->get('insert')->prefix);
|
||||
$queryModule->insert('module_id', 'module_theme', 'module_path', 'module_active', 'module_version')
|
||||
->into('module')
|
||||
->values($info->getInternalName(), 'Default', $info->getDirectory(), 0, $info->getVersion())
|
||||
->execute();
|
||||
|
||||
$sth = $dbPool->get()->con->prepare(
|
||||
'INSERT INTO `' . $dbPool->get()->prefix . 'module` (`module_id`, `module_theme`, `module_path`, `module_active`, `module_version`) VALUES
|
||||
(:internal, :theme, :path, :active, :version);'
|
||||
$queryLoad = new Builder($dbPool->get('insert'));
|
||||
$queryLoad->prefix($dbPool->get('insert')->prefix);
|
||||
$queryLoad->insert('module_load_pid', 'module_load_type', 'module_load_from', 'module_load_for', 'module_load_file')
|
||||
->into('module_load');
|
||||
|
||||
$load = $info->getLoad();
|
||||
foreach ($load as $val) {
|
||||
foreach ($val['pid'] as $pid) {
|
||||
$queryLoad->values(
|
||||
sha1(\str_replace('/', '', $pid)),
|
||||
(int) $val['type'],
|
||||
$val['from'],
|
||||
$val['for'],
|
||||
$val['file']
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
$sth->bindValue(':internal', $info->getInternalName(), \PDO::PARAM_INT);
|
||||
$sth->bindValue(':theme', 'Default', \PDO::PARAM_STR);
|
||||
$sth->bindValue(':path', $info->getDirectory(), \PDO::PARAM_STR);
|
||||
$sth->bindValue(':active', 0, \PDO::PARAM_INT);
|
||||
$sth->bindValue(':version', $info->getVersion(), \PDO::PARAM_STR);
|
||||
$sth->execute();
|
||||
|
||||
$sth = $dbPool->get()->con->prepare(
|
||||
'INSERT INTO `' . $dbPool->get()->prefix . 'module_load` (`module_load_pid`, `module_load_type`, `module_load_from`, `module_load_for`, `module_load_file`) VALUES
|
||||
(:pid, :type, :from, :for, :file);'
|
||||
);
|
||||
|
||||
$load = $info->getLoad();
|
||||
foreach ($load as $val) {
|
||||
foreach ($val['pid'] as $pid) {
|
||||
$sth->bindValue(':pid', sha1(\str_replace('/', '', $pid)), \PDO::PARAM_STR);
|
||||
$sth->bindValue(':type', $val['type'], \PDO::PARAM_INT);
|
||||
$sth->bindValue(':from', $val['from'], \PDO::PARAM_STR);
|
||||
$sth->bindValue(':for', $val['for'], \PDO::PARAM_STR);
|
||||
$sth->bindValue(':file', $val['file'], \PDO::PARAM_STR);
|
||||
$sth->execute();
|
||||
}
|
||||
}
|
||||
|
||||
$dbPool->get()->con->commit();
|
||||
break;
|
||||
default:
|
||||
throw new InvalidDatabaseTypeException($dbPool->get()->getType());
|
||||
if (!empty($queryLoad->getValues())) {
|
||||
$queryLoad->execute();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ declare(strict_types=1);
|
|||
|
||||
namespace phpOMS\Module;
|
||||
|
||||
use phpOMS\DataStorage\Database\Query\Builder;
|
||||
use phpOMS\DataStorage\Database\DatabaseType;
|
||||
use phpOMS\DataStorage\Database\DatabasePool;
|
||||
use phpOMS\DataStorage\Database\Exception\InvalidDatabaseTypeException;
|
||||
|
|
@ -72,24 +73,12 @@ class StatusAbstract
|
|||
*/
|
||||
public static function activateInDatabase(DatabasePool $dbPool, InfoManager $info) : void
|
||||
{
|
||||
switch ($dbPool->get()->getType()) {
|
||||
case DatabaseType::MYSQL:
|
||||
$dbPool->get()->con->beginTransaction();
|
||||
|
||||
$sth = $dbPool->get()->con->prepare(
|
||||
'UPDATE `' . $dbPool->get()->prefix . 'module` SET `module_active` = :active WHERE `module_id` = :internal;'
|
||||
);
|
||||
|
||||
$sth->bindValue(':internal', $info->getInternalName(), \PDO::PARAM_INT);
|
||||
$sth->bindValue(':active', 1, \PDO::PARAM_INT);
|
||||
$sth->execute();
|
||||
|
||||
$dbPool->get()->con->commit();
|
||||
|
||||
break;
|
||||
default:
|
||||
throw new InvalidDatabaseTypeException($dbPool->get()->getType());
|
||||
}
|
||||
$query = new Builder($dbPool->get('update'));
|
||||
$query->prefix($dbPool->get('update')->prefix);
|
||||
$query->update('module')
|
||||
->sets('module.module_active', 1)
|
||||
->where('module.module_id', '=', $info->getInternalName())
|
||||
->execute();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -135,23 +124,11 @@ class StatusAbstract
|
|||
*/
|
||||
public static function deactivateInDatabase(DatabasePool $dbPool, InfoManager $info) : void
|
||||
{
|
||||
switch ($dbPool->get()->getType()) {
|
||||
case DatabaseType::MYSQL:
|
||||
$dbPool->get()->con->beginTransaction();
|
||||
|
||||
$sth = $dbPool->get()->con->prepare(
|
||||
'UPDATE `' . $dbPool->get()->prefix . 'module` SET `module_active` = :active WHERE `module_id` = :internal;'
|
||||
);
|
||||
|
||||
$sth->bindValue(':internal', $info->getInternalName(), \PDO::PARAM_INT);
|
||||
$sth->bindValue(':active', 0, \PDO::PARAM_INT);
|
||||
$sth->execute();
|
||||
|
||||
$dbPool->get()->con->commit();
|
||||
|
||||
break;
|
||||
default:
|
||||
throw new InvalidDatabaseTypeException($dbPool->get()->getType());
|
||||
}
|
||||
$query = new Builder($dbPool->get('update'));
|
||||
$query->prefix($dbPool->get('update')->prefix);
|
||||
$query->update('module')
|
||||
->sets('module.module_active', 0)
|
||||
->where('module.module_id', '=', $info->getInternalName())
|
||||
->execute();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -120,5 +120,7 @@ $GLOBALS['session'] = $httpSession;
|
|||
$GLOBALS['dbpool'] = new DatabasePool();
|
||||
$GLOBALS['dbpool']->create('admin', $CONFIG['db']['core']['masters']['admin']);
|
||||
$GLOBALS['dbpool']->create('select', $CONFIG['db']['core']['masters']['select']);
|
||||
$GLOBALS['dbpool']->create('insert', $CONFIG['db']['core']['masters']['insert']);
|
||||
$GLOBALS['dbpool']->create('update', $CONFIG['db']['core']['masters']['update']);
|
||||
|
||||
DataMapperAbstract::setConnection($GLOBALS['dbpool']->get());
|
||||
|
|
|
|||
|
|
@ -254,11 +254,19 @@ class BuilderTest extends \PHPUnit\Framework\TestCase
|
|||
$sql = 'INSERT INTO `a` VALUES (1, \'test\');';
|
||||
self::assertEquals($sql, $query->insert()->into('a')->values(1, 'test')->toSql());
|
||||
|
||||
$query = new Builder($this->con);
|
||||
$sql = 'INSERT INTO `a` VALUES (1, \'test\');';
|
||||
self::assertEquals($sql, $query->insert()->into('a')->value([1, 'test'])->toSql());
|
||||
|
||||
$query = new Builder($this->con);
|
||||
$sql = 'INSERT INTO `a` (`test`, `test2`) VALUES (1, \'test\');';
|
||||
self::assertEquals($sql, $query->insert('test', 'test2')->into('a')->values(1, 'test')->toSql());
|
||||
self::assertEquals([[1, 'test']], $query->getValues());
|
||||
|
||||
$query = new Builder($this->con);
|
||||
$sql = 'INSERT INTO `a` (`test`, `test2`) VALUES (1, \'test\'), (2, \'test2\');';
|
||||
self::assertEquals($sql, $query->insert('test', 'test2')->into('a')->values(1, 'test')->values(2, 'test2')->toSql());
|
||||
|
||||
$query = new Builder($this->con);
|
||||
$sql = 'INSERT INTO `a` (`test`, `test2`) VALUES (:test, :test2);';
|
||||
self::assertEquals($sql, $query->insert('test', 'test2')->into('a')->values(':test', ':test2')->toSql());
|
||||
|
|
@ -281,6 +289,10 @@ class BuilderTest extends \PHPUnit\Framework\TestCase
|
|||
$sql = 'UPDATE `a` SET `a`.`test` = 1, `a`.`test2` = 2 WHERE `a`.`test` = 1;';
|
||||
self::assertEquals($sql, $query->update('a')->set(['a.test' => 1])->set(['a.test2' => 2])->where('a.test', '=', 1)->toSql());
|
||||
|
||||
$query = new Builder($this->con);
|
||||
$sql = 'UPDATE `a` SET `a`.`test` = 1, `a`.`test2` = 2 WHERE `a`.`test` = 1;';
|
||||
self::assertEquals($sql, $query->update('a')->sets('a.test', 1)->sets('a.test2', 2)->where('a.test', '=', 1)->toSql());
|
||||
|
||||
$query = new Builder($this->con);
|
||||
$sql = 'UPDATE `a` SET `a`.`test` = 1, `a`.`test2` = :test2 WHERE `a`.`test` = :test3;';
|
||||
self::assertEquals($sql, $query->update('a')->set(['a.test' => 1])->set(['a.test2' => ':test2'])->where('a.test', '=', ':test3')->toSql());
|
||||
|
|
|
|||
|
|
@ -42,5 +42,21 @@ class MetaTest extends \PHPUnit\Framework\TestCase
|
|||
|
||||
$meta->setDescription('some description');
|
||||
self::assertEquals('some description', $meta->getDescription());
|
||||
|
||||
$meta->setProperty('og:title', 'TestProperty');
|
||||
$meta->setItemprop('title', 'TestItemprop');
|
||||
$meta->setName('title', 'TestName');
|
||||
|
||||
self::assertEquals(
|
||||
'<meta name="keywords" content="orange">'
|
||||
. '<meta name="author" content="oms">'
|
||||
. '<meta name="description" content="some description">'
|
||||
. '<meta charset="utf-8">'
|
||||
. '<meta name="generator" content="Orange Management">'
|
||||
. '<meta property="og:title" content="TestProperty">'
|
||||
. '<meta itemprop="title" content="TestItemprop">'
|
||||
. '<meta name="title" content="TestName">',
|
||||
$meta->render()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user