Skip to content

MaterialItem.MaterialList is nullable #7217

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Apr 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions api/migrations/schema/Version20250412165829.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20250412165829 extends AbstractMigration {
public function getDescription(): string {
return 'MaterialItem.MaterialList is nullable';
}

public function up(Schema $schema): void {
// this up() migration is auto-generated, please modify it to your needs
$this->addSql(<<<'SQL'
ALTER TABLE material_item ALTER materialListId DROP NOT NULL
SQL);
}

public function down(Schema $schema): void {
// this down() migration is auto-generated, please modify it to your needs
$this->addSql(<<<'SQL'
ALTER TABLE material_item ALTER materiallistid SET NOT NULL
SQL);
}
}
13 changes: 12 additions & 1 deletion api/src/Entity/ContentNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
#[ORM\InheritanceType('SINGLE_TABLE')]
#[ORM\DiscriminatorColumn(name: 'strategy', type: 'string')]
#[ORM\UniqueConstraint(name: 'contentnode_parentid_slot_position_unique', columns: ['parentid', 'slot', 'position'])]
abstract class ContentNode extends BaseEntity implements BelongsToContentNodeTreeInterface, CopyFromPrototypeInterface, HasParentInterface {
abstract class ContentNode extends BaseEntity implements BelongsToCampInterface, BelongsToContentNodeTreeInterface, CopyFromPrototypeInterface, HasParentInterface {
use ClassInfoTrait;

/**
Expand Down Expand Up @@ -193,6 +193,17 @@ public function setParent(?ContentNode $parent) {
$this->root ??= $parent?->root;
}

public function getCamp(): ?Camp {
if (null == $this->getRoot()) {
return null;
}
if ($this->getRoot()->campRootContentNodes->count() > 0) {
return $this->getRoot()->campRootContentNodes[0]->camp;
}

return null;
}

/**
* Holds the actual data of the content node.
*/
Expand Down
26 changes: 19 additions & 7 deletions api/src/Entity/MaterialItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Serializer\Attribute\SerializedName;
use Symfony\Component\Validator\Constraints as Assert;

/**
Expand All @@ -44,6 +45,7 @@
security: 'is_authenticated()'
),
new Post(
denormalizationContext: ['groups' => ['write', 'create']],
securityPostDenormalize: 'is_granted("CAMP_MEMBER", object) or is_granted("CAMP_MANAGER", object) or object.materialList === null'
),
],
Expand All @@ -58,11 +60,12 @@ class MaterialItem extends BaseEntity implements BelongsToCampInterface, CopyFro
* The list to which this item belongs. Lists are used to keep track of who is
* responsible to prepare and bring the item to the camp.
*/
#[AssertBelongsToSameCamp(compareToPrevious: true, groups: ['update'])]
#[Assert\NotNull]
#[AssertBelongsToSameCamp]
#[ApiProperty(example: '/material_lists/1a2b3c4d')]
#[Groups(['read', 'write'])]
#[ORM\ManyToOne(targetEntity: MaterialList::class, inversedBy: 'materialItems')]
#[ORM\JoinColumn(nullable: false, onDelete: 'cascade')]
#[ORM\JoinColumn(nullable: true, onDelete: 'cascade')]
public ?MaterialList $materialList = null;

/**
Expand Down Expand Up @@ -131,9 +134,13 @@ public function __construct() {
$this->periodMaterialItems = new ArrayCollection();
}

#[ApiProperty(readable: false)]
#[ApiProperty]
#[SerializedName('camp')]
#[Groups(['read'])]
public function getCamp(): ?Camp {
return $this->materialList?->getCamp();
return $this->period?->getCamp()
?? $this->materialNode?->getCamp()
?? $this->materialList?->getCamp();
}

/**
Expand All @@ -143,9 +150,14 @@ public function getCamp(): ?Camp {
public function copyFromPrototype($prototype, $entityMap): void {
$entityMap->add($prototype, $this);

/** @var MaterialList $materialList */
$materialList = $entityMap->get($prototype->materialList);
$materialList->addMaterialItem($this);
if (null != $prototype->materialList) {
/** @var MaterialList $materialList */
$materialList = $entityMap->get($prototype->materialList);

if ($entityMap->belongsToTargetCamp($materialList)) {
$materialList->addMaterialItem($this);
}
}

$this->article = $prototype->article;
$this->quantity = $prototype->quantity;
Expand Down
5 changes: 3 additions & 2 deletions api/src/Repository/MaterialItemRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public function __construct(ManagerRegistry $registry) {
}

public function filterByUser(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, User $user): void {
$materialList = QueryBuilderHelper::findOrAddInnerRootJoinAlias($queryBuilder, $queryNameGenerator, 'materialList');
$this->filterByCampCollaboration($queryBuilder, $user, "{$materialList}.camp");
$periodMaterialItems = QueryBuilderHelper::findOrAddInnerRootJoinAlias($queryBuilder, $queryNameGenerator, 'periodMaterialItems');
$period = QueryBuilderHelper::findOrAddInnerJoinAlias($queryBuilder, $queryNameGenerator, $periodMaterialItems, 'period');
$this->filterByCampCollaboration($queryBuilder, $user, "{$period}.camp");
}
}
3 changes: 2 additions & 1 deletion api/src/State/ActivityCreateProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function onBefore($data, Operation $operation, array $uriVariables = [],
throw new \UnexpectedValueException('Property rootContentNode of provided category is of wrong type. Object of type '.ColumnLayout::class.' expected.');
}

$targetCamp = $data->category->camp;
$data->camp = $data->category->camp;
$rootContentNodePrototype = $data->category->rootContentNode;

Expand All @@ -50,7 +51,7 @@ public function onBefore($data, Operation $operation, array $uriVariables = [],
$data->setRootContentNode($rootContentNode);

// deep copy from category root node
$entityMap = new EntityMap();
$entityMap = new EntityMap($targetCamp);
$rootContentNode->copyFromPrototype($rootContentNodePrototype, $entityMap);

return $data;
Expand Down
2 changes: 1 addition & 1 deletion api/src/State/CampCreateProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function onBefore($data, Operation $operation, array $uriVariables = [],

// copy from prototype, if given
if (isset($data->campPrototype)) {
$entityMap = new EntityMap();
$entityMap = new EntityMap($data);
$data->copyFromPrototype($data->campPrototype, $entityMap);
}

Expand Down
2 changes: 1 addition & 1 deletion api/src/State/CategoryCreateProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function onBefore($data, Operation $operation, array $uriVariables = [],

if (isset($data->copyCategorySource)) {
// CopyActivity Source is set -> copy it's content (rootContentNode)
$entityMap = new EntityMap();
$entityMap = new EntityMap($data->camp);
$rootContentNode->copyFromPrototype($data->copyCategorySource->getRootContentNode(), $entityMap);
}

Expand Down
2 changes: 1 addition & 1 deletion api/src/State/ChecklistCreateProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function __construct(ProcessorInterface $decorated) {
public function onBefore($data, Operation $operation, array $uriVariables = [], array $context = []): Checklist {
if (isset($data->copyChecklistSource)) {
// CopyChecklist Source is set -> copy it's content
$entityMap = new EntityMap();
$entityMap = new EntityMap($data->camp);
$data->copyFromPrototype($data->copyChecklistSource, $entityMap);
}

Expand Down
8 changes: 8 additions & 0 deletions api/src/Util/EntityMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
namespace App\Util;

use App\Entity\BaseEntity;
use App\Entity\BelongsToCampInterface;
use App\Entity\Camp;

class EntityMap {
use ClassInfoTrait;

private $map = [];

public function __construct(private Camp $targetCamp) {}

public function add(BaseEntity $prototype, BaseEntity $entity) {
$key = $this->getObjectClass($prototype).'#'.$prototype->getId();
$this->map[$key] = $entity;
Expand All @@ -20,4 +24,8 @@ public function get(BaseEntity $prototype): BaseEntity {

return $keyExists ? $this->map[$key] : $prototype;
}

public function belongsToTargetCamp(BelongsToCampInterface $entity) {
return $entity->getCamp() == $this->targetCamp;
}
}
17 changes: 16 additions & 1 deletion api/tests/Api/Activities/CreateActivityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ public function testCreateActivityFromCopySourceWithinSameCamp() {
}

public function testCreateActivityFromCopySourceAcrossCamp() {
static::createClientWithCredentials()->request(
$response = static::createClientWithCredentials()->request(
'POST',
'/activities',
['json' => $this->getExampleWritePayload(
Expand All @@ -540,6 +540,21 @@ public function testCreateActivityFromCopySourceAcrossCamp() {

// Activity created
$this->assertResponseStatusCodeSame(201);

// Embedded MaterialNode -> MaterialItems
// Test MaterialList is nulled
$responseArray = $response->toArray();
$contentNodes = $responseArray['_embedded']['contentNodes'];

$materialNodes = array_filter($contentNodes, fn ($cn) => 'Material' == $cn['contentTypeName']);
$this->assertCount(1, $materialNodes);

$materialNode = reset($materialNodes);
$materialItems = $materialNode['_embedded']['materialItems'];
$this->assertCount(1, $materialItems);

$materailItem = reset($materialItems);
$this->assertNull($materailItem['_links']['materialList']);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions api/tests/Api/MaterialItems/CreateMaterialItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public function testCreateMaterialItemValidatesPeriodFromDifferentCamp() {
$this->assertJsonContains([
'violations' => [
[
'propertyPath' => 'period',
'propertyPath' => 'materialList',
'message' => 'Must belong to the same camp.',
],
],
Expand All @@ -222,7 +222,7 @@ public function testCreateMaterialItemValidatesMaterialNodeFromDifferentCamp() {
$this->assertJsonContains([
'violations' => [
[
'propertyPath' => 'materialNode',
'propertyPath' => 'materialList',
'message' => 'Must belong to the same camp.',
],
],
Expand Down
8 changes: 4 additions & 4 deletions api/tests/Api/MaterialItems/UpdateMaterialItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,9 @@ public function testPatchMaterialItemValidatesMissingMaterialList() {
'materialList' => null,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The following test would be nice:
update a materialitem with a null material list

-> if you set the materiallist to not null -> success
-> if you change another property and leave the materiallist null -> error
-> if you change the material list from null to a material list of another camp -> error

], 'headers' => ['Content-Type' => 'application/merge-patch+json']]);

$this->assertResponseStatusCodeSame(400);
$this->assertResponseStatusCodeSame(422);
$this->assertJsonContains([
'detail' => 'The type of the "materialList" attribute must be "array" (nested document) or "string" (IRI), "NULL" given.',
'detail' => 'materialList: This value should not be null.',
]);
}

Expand Down Expand Up @@ -244,7 +244,7 @@ public function testPatchMaterialItemValidatesPeriodFromDifferentCamp() {
$this->assertJsonContains([
'violations' => [
[
'propertyPath' => 'period',
'propertyPath' => 'materialList',
'message' => 'Must belong to the same camp.',
],
],
Expand All @@ -261,7 +261,7 @@ public function testPatchMaterialItemValidatesMaterialNodeFromDifferentCamp() {
$this->assertJsonContains([
'violations' => [
[
'propertyPath' => 'materialNode',
'propertyPath' => 'materialList',
'message' => 'Must belong to the same camp.',
],
],
Expand Down
4 changes: 2 additions & 2 deletions api/tests/Api/SnapshotTests/EndpointPerformanceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,9 @@ protected function getSnapshotId(): string {

private static function getContentNodeEndpointQueryCountRanges(): array {
return [
'/content_nodes' => [8, 11],
'/content_nodes' => [10, 12],
'/content_node/column_layouts' => [6, 6],
'/content_node/column_layouts/item' => [10, 10],
'/content_node/column_layouts/item' => [9, 9],
'/content_node/checklist_nodes' => [6, 7],
'/content_node/checklist_nodes/item' => [9, 9],
'/content_node/material_nodes' => [6, 7],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
"materialItems": [
{
"_links": {
"camp": {
"href": "escaped_value"
},
"materialList": {
"href": "escaped_value"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
"materialItems": [
{
"_links": {
"camp": {
"href": "escaped_value"
},
"materialList": {
"href": "escaped_value"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"items": [
{
"_links": {
"camp": {
"href": "escaped_value"
},
"materialList": {
"href": "escaped_value"
},
Expand All @@ -21,6 +24,9 @@
},
{
"_links": {
"camp": {
"href": "escaped_value"
},
"materialList": {
"href": "escaped_value"
},
Expand All @@ -39,6 +45,9 @@
},
{
"_links": {
"camp": {
"href": "escaped_value"
},
"materialList": {
"href": "escaped_value"
},
Expand All @@ -57,6 +66,9 @@
},
{
"_links": {
"camp": {
"href": "escaped_value"
},
"materialList": {
"href": "escaped_value"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
"materialItems": [
{
"_links": {
"camp": {
"href": "escaped_value"
},
"materialList": {
"href": "escaped_value"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"_links": {
"camp": {
"href": "escaped_value"
},
"materialList": {
"href": "escaped_value"
},
Expand Down
Loading
Loading