Skip to content

Commit

Permalink
#204 Fix problems with some person related tests due to not nullable …
Browse files Browse the repository at this point in the history
…Document fields.
  • Loading branch information
haogatyp committed Mar 23, 2022
1 parent 1da135a commit 68f0d91
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
4 changes: 2 additions & 2 deletions library/Opus/Db2/PersonRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public function getAllPersonsCount($role = null, $filter = null)
*/
public function getDocumentsByRole($person, $role)
{
if ($person->getId() === 0) {
if ($person->isNewRecord()) {
return [];
}

Expand Down Expand Up @@ -214,7 +214,7 @@ public function getDocumentsByRole($person, $role)
*/
public function getDocumentIds($person, $role = null)
{
if ($person->getId() === 0) {
if ($person->isNewRecord()) {
// TODO do more?
return [];
}
Expand Down
10 changes: 10 additions & 0 deletions library/Opus/Model2/AbstractModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,16 @@ public function store()
return $modelId;
}

/**
* Returns whether model is a new record.
*
* @return bool
*/
public function isNewRecord()
{
return $this->getId() === 0 || $this->getId() === null;
}

/**
* Remove the model instance from the database.
*
Expand Down
44 changes: 41 additions & 3 deletions library/Opus/Model2/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@
use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\Mapping as ORM;

use http\Exception\InvalidArgumentException;
use InvalidArgumentException;
use Opus\Date;
use Opus\Model\DbException;
use Opus\Model\ModelException;

/**
Expand All @@ -58,6 +59,20 @@
*/
class Document extends AbstractModel
{
const STATE_DELETED = 'deleted';

const STATE_INPROGRESS = 'inprogress';

const STATE_RESTRICTED = 'restricted';

const STATE_UNPUBLISHED = 'unpublished';

const STATE_PUBLISHED = 'published';

const STATE_TEMPORARY = 'temporary';

const STATE_AUDITED = 'audited';

/**
* @ORM\Id
* @ORM\Column(type="integer")
Expand Down Expand Up @@ -186,7 +201,7 @@ class Document extends AbstractModel
* @ORM\Column(type="string", name="publication_state", columnDefinition="ENUM('draft','accepted','submitted','published','updated')")
* @var string
*/
protected $publicationState;
protected $publicationState = 'draft';

/**
* @ORM\Column(type="opusDate", name="server_date_created")
Expand Down Expand Up @@ -216,7 +231,7 @@ class Document extends AbstractModel
* @ORM\Column(type="string", name="server_state", columnDefinition="ENUM('audited','published','restricted','inprogress','unpublished','deleted','temporary')")
* @var string
*/
private $serverState;
private $serverState = self::STATE_TEMPORARY;

/**
* @ORM\Column(type="string")
Expand Down Expand Up @@ -253,6 +268,29 @@ public function __construct() {
$this->documentPersons = new ArrayCollection();
}

/**
* Perform any actions needed to provide storing.
*
* @return mixed|null Anything else than null will cancel the storage process.
*/
protected function preStore()
{
$result = parent::preStore();

$date = new Date();
$date->setNow();

if ($this->isNewRecord()) {
if ($this->getServerDateCreated() === null) {
$this->setServerDateCreated($date);
}
}
$this->setServerDateModified($date);

return $result;
}


/**
* @return int
*/
Expand Down

0 comments on commit 68f0d91

Please sign in to comment.