diff --git a/app/Jobs/Emails/Registration/PromoCodes/SponsorPromoCodeEmail.php b/app/Jobs/Emails/Registration/PromoCodes/SponsorPromoCodeEmail.php index 40be83558..572a4b1e7 100644 --- a/app/Jobs/Emails/Registration/PromoCodes/SponsorPromoCodeEmail.php +++ b/app/Jobs/Emails/Registration/PromoCodes/SponsorPromoCodeEmail.php @@ -54,7 +54,7 @@ public function __construct $summit = $promo_code->getSummit(); $payload = []; $sponsor = $promo_code->getSponsor(); - $payload[IMailTemplatesConstants::sponsor_tier_name] = $sponsor->getSponsorship()->getType()->getName(); + $payload[IMailTemplatesConstants::sponsor_tier_name] = implode(',', $sponsor->getSponsorshipTierNames()); $payload[IMailTemplatesConstants::promo_code] = $promo_code->getCode(); $payload[IMailTemplatesConstants::company_name] = ''; $company = $sponsor->getCompany(); diff --git a/app/ModelSerializers/SerializerRegistry.php b/app/ModelSerializers/SerializerRegistry.php index 4b0631b00..3c5f50941 100644 --- a/app/ModelSerializers/SerializerRegistry.php +++ b/app/ModelSerializers/SerializerRegistry.php @@ -115,6 +115,8 @@ use App\ModelSerializers\Summit\SummitScheduleConfigSerializer; use App\ModelSerializers\Summit\SummitSchedulePreFilterElementConfigSerializer; use App\ModelSerializers\Summit\SummitSignSerializer; +use App\ModelSerializers\Summit\SummitSponsorshipAddOnSerializer; +use App\ModelSerializers\Summit\SummitSponsorshipSerializer; use App\ModelSerializers\Summit\SummitSponsorshipTypeSerializer; use App\ModelSerializers\Summit\TrackTagGroups\TrackTagGroupAllowedTagSerializer; use App\ModelSerializers\Summit\TrackTagGroups\TrackTagGroupSerializer; @@ -566,6 +568,8 @@ private function __construct() // summit sponsors $this->registry['SponsorshipType'] = SponsorshipTypeSerializer::class; + $this->registry['SummitSponsorship'] = SummitSponsorshipSerializer::class; + $this->registry['SummitSponsorshipAddOn'] = SummitSponsorshipAddOnSerializer::class; $this->registry['SummitSponsorshipType'] = SummitSponsorshipTypeSerializer::class; $this->registry['Sponsor'] = SponsorSerializer::class; $this->registry['SponsorAd'] = SponsorAdSerializer::class; diff --git a/app/ModelSerializers/Summit/SponsorSerializer.php b/app/ModelSerializers/Summit/SponsorSerializer.php index 2d311a9b3..e119db057 100644 --- a/app/ModelSerializers/Summit/SponsorSerializer.php +++ b/app/ModelSerializers/Summit/SponsorSerializer.php @@ -49,6 +49,7 @@ final class SponsorSerializer extends SilverStripeSerializer protected static $allowed_relations = [ 'extra_questions', 'members', + 'sponsorships', ]; /** @@ -80,6 +81,14 @@ public function serialize($expand = null, array $fields = [], array $relations = $values['members'] = $members; } + if (in_array('sponsorships', $relations)) { + $sponsorships = []; + foreach ($sponsor->getSponsorships() as $sponsorship) { + $sponsorships[] = $sponsorship->getId(); + } + $values['sponsorships'] = $sponsorships; + } + if (!empty($expand)) { $exp_expand = explode(',', $expand); foreach ($exp_expand as $relation) { @@ -163,17 +172,22 @@ public function serialize($expand = null, array $fields = [], array $relations = } } break; - case 'sponsorship': + case 'sponsorships': { - if ($sponsor->hasSponsorship()) { - unset($values['sponsorship_id']); - $values['sponsorship'] = SerializerRegistry::getInstance()->getSerializer($sponsor->getSponsorship())->serialize - ( - AbstractSerializer::filterExpandByPrefix($expand, $relation), - AbstractSerializer::filterFieldsByPrefix($fields, $relation), - AbstractSerializer::filterFieldsByPrefix($relations, $relation), - $params - ); + $sponsorships = $sponsor->getSponsorships(); + if (count($sponsorships) > 0) { + unset($values['sponsorships']); + $summit_sponsorships = []; + foreach ($sponsorships as $sponsorship) { + $summit_sponsorships[] = SerializerRegistry::getInstance()->getSerializer($sponsorship)->serialize + ( + AbstractSerializer::filterExpandByPrefix($expand, $relation), + AbstractSerializer::filterFieldsByPrefix($fields, $relation), + AbstractSerializer::filterFieldsByPrefix($relations, $relation), + $params + ); + } + $values['sponsorships'] = $summit_sponsorships; } } break; diff --git a/app/ModelSerializers/Summit/SummitSponsorshipAddOnSerializer.php b/app/ModelSerializers/Summit/SummitSponsorshipAddOnSerializer.php new file mode 100644 index 000000000..f72492086 --- /dev/null +++ b/app/ModelSerializers/Summit/SummitSponsorshipAddOnSerializer.php @@ -0,0 +1,28 @@ + 'name:json_string', + 'Type' => 'type:json_string' + ]; +} \ No newline at end of file diff --git a/app/ModelSerializers/Summit/SummitSponsorshipSerializer.php b/app/ModelSerializers/Summit/SummitSponsorshipSerializer.php new file mode 100644 index 000000000..752e37b6f --- /dev/null +++ b/app/ModelSerializers/Summit/SummitSponsorshipSerializer.php @@ -0,0 +1,72 @@ + 'type_id:json_int' + ]; + + protected static $allowed_relations = [ + 'add_ons', + ]; + + /** + * @param null $expand + * @param array $fields + * @param array $relations + * @param array $params + * @return array + */ + public function serialize($expand = null, array $fields = [], array $relations = [], array $params = []) + { + $sponsorship = $this->object; + if (!$sponsorship instanceof SummitSponsorship) return []; + $values = parent::serialize($expand, $fields, $relations, $params); + + if (in_array('add_ons', $relations) && !isset($values['add_ons'])) { + $add_ons = []; + foreach ($sponsorship->getAddOns() as $add_on) { + $add_ons[] = $add_on->getId(); + } + $values['add_ons'] = $add_ons; + } + return $values; + } + + protected static $expand_mappings = [ + 'add_ons' => [ + 'type' => Many2OneExpandSerializer::class, + 'getter' => 'getAddOns', + ], + 'type' => [ + 'type' => One2ManyExpandSerializer::class, + 'original_attribute' => 'type_id', + 'getter' => 'getType', + 'has' => 'hasType' + ], + ]; +} \ No newline at end of file diff --git a/app/Models/Foundation/Summit/Factories/SponsorFactory.php b/app/Models/Foundation/Summit/Factories/SponsorFactory.php index bed130e67..31fd10754 100644 --- a/app/Models/Foundation/Summit/Factories/SponsorFactory.php +++ b/app/Models/Foundation/Summit/Factories/SponsorFactory.php @@ -11,6 +11,8 @@ * See the License for the specific language governing permissions and * limitations under the License. **/ + +use models\exceptions\ValidationException; use models\summit\Sponsor; /** * Class SponsorFactory @@ -36,9 +38,6 @@ public static function populate(Sponsor $sponsor, array $data):Sponsor{ if(isset($data['company'])) $sponsor->setCompany($data['company']); - if(isset($data['sponsorship'])) - $sponsor->setSponsorship($data['sponsorship']); - if(isset($data['featured_event'])) $sponsor->setFeaturedEvent($data['featured_event']); diff --git a/app/Models/Foundation/Summit/Sponsor.php b/app/Models/Foundation/Summit/Sponsor.php index 9973ee73d..2fa8d2ffc 100644 --- a/app/Models/Foundation/Summit/Sponsor.php +++ b/app/Models/Foundation/Summit/Sponsor.php @@ -51,7 +51,6 @@ class Sponsor extends SilverstripeBaseModel implements IOrderable 'getCarouselAdvertiseImageId' => 'carousel_advertise_image', 'getFeaturedEventId' => 'featured_event', 'getCompanyId' => 'company', - 'getSponsorshipId' => 'sponsorship', ]; protected $hasPropertyMappings = [ @@ -61,7 +60,6 @@ class Sponsor extends SilverstripeBaseModel implements IOrderable 'hasCarouselAdvertiseImage' => 'carousel_advertise_image', 'hasFeaturedEvent' => 'featured_event', 'hasCompany' => 'company', - 'hasSponsorship' => 'sponsorship', ]; /** @@ -90,13 +88,6 @@ class Sponsor extends SilverstripeBaseModel implements IOrderable #[ORM\Column(name: 'IsPublished', type: 'boolean')] protected $is_published; - /** - * @var SummitSponsorshipType - */ - #[ORM\JoinColumn(name: 'SummitSponsorshipTypeID', referencedColumnName: 'ID', onDelete: 'SET NULL')] - #[ORM\ManyToOne(targetEntity: \SummitSponsorshipType::class)] - protected $sponsorship; - /** * @var SponsorUserInfoGrant[] */ @@ -230,6 +221,12 @@ class Sponsor extends SilverstripeBaseModel implements IOrderable #[ORM\OneToOne(targetEntity: \models\summit\SummitLeadReportSetting::class, mappedBy: 'sponsor', cascade: ['persist', 'remove'], orphanRemoval: true, fetch: 'EXTRA_LAZY')] private $lead_report_setting; + /** + * @var SummitSponsorship[] + */ + #[ORM\OneToMany(targetEntity: \models\summit\SummitSponsorship::class, mappedBy: 'sponsor', cascade: ['persist', 'remove'], orphanRemoval: true, fetch: 'EXTRA_LAZY')] + private $sponsorships; + /** * Sponsor constructor. */ @@ -243,7 +240,8 @@ public function __construct() $this->ads = new ArrayCollection(); $this->is_published = true; $this->show_logo_in_event_page = true; - $this->extra_questions = new ArrayCollection; + $this->extra_questions = new ArrayCollection(); + $this->sponsorships = new ArrayCollection(); } public static function getAllowedQuestionTypes(): array @@ -289,22 +287,6 @@ public function setCompany(Company $company): void $this->company = $company; } - /** - * @return SummitSponsorshipType - */ - public function getSponsorship(): ?SummitSponsorshipType - { - return $this->sponsorship; - } - - /** - * @param SummitSponsorshipType $sponsorship - */ - public function setSponsorship(SummitSponsorshipType $sponsorship): void - { - $this->sponsorship = $sponsorship; - } - /** * @return Member[] */ @@ -957,4 +939,49 @@ public function clearLeadReportSetting() $this->lead_report_setting->clearSponsor(); $this->lead_report_setting = null; } + + + /** + * @return SummitSponsorship[] + */ + public function getSponsorships() + { + return $this->sponsorships; + } + + /** + * @param SummitSponsorship $sponsorship + * @throws ValidationException + */ + public function addSponsorship(SummitSponsorship $sponsorship): void + { + $sponsorship_type = $sponsorship->getType(); + $criteria = Criteria::create(); + $criteria->where(Criteria::expr()->eq('type', $sponsorship_type)); + if ($this->sponsorships->matching($criteria)->count() > 0) { + throw new ValidationException( + sprintf('Sponsor %s already has a sponsorship of the same type (%s).', + $this->id, $sponsorship_type->getId())); + } + $sponsorship->setSponsor($this); + $this->sponsorships->add($sponsorship); + } + + /** + * @return void + */ + public function clearSponsorships(): void + { + if (is_null($this->sponsorships)) return; + $this->sponsorships->clear(); + $this->sponsorships = null; + } + + /** + * @return array + */ + public function getSponsorshipTierNames(): array + { + return array_map(fn($sponsorship) => $sponsorship->getType()->getType()->getName(), $this->sponsorships); + } } \ No newline at end of file diff --git a/app/Models/Foundation/Summit/SummitSponsorship.php b/app/Models/Foundation/Summit/SummitSponsorship.php new file mode 100644 index 000000000..077a23062 --- /dev/null +++ b/app/Models/Foundation/Summit/SummitSponsorship.php @@ -0,0 +1,102 @@ + 'type', + ]; + + protected $hasPropertyMappings = [ + 'hasType' => 'type', + ]; + + /** + * @var Sponsor + */ + #[ORM\JoinColumn(name: 'SponsorID', referencedColumnName: 'ID')] + #[ORM\ManyToOne(targetEntity: Sponsor::class)] + protected $sponsor; + + /** + * @var SummitSponsorshipAddOn[] + */ + #[ORM\OneToMany(mappedBy: 'sponsorship', targetEntity: SummitSponsorshipAddOn::class, cascade: ['persist', 'remove'], fetch: 'EXTRA_LAZY', orphanRemoval: true)] + private $add_ons; + + /** + * @var SummitSponsorshipType + */ + #[ORM\JoinColumn(name: 'TypeID', referencedColumnName: 'ID', onDelete: 'SET NULL')] + #[ORM\ManyToOne(targetEntity: SummitSponsorshipType::class)] + protected $type; + + public function __construct() + { + parent::__construct(); + $this->add_ons = new ArrayCollection(); + } + + public function getSponsor(): Sponsor + { + return $this->sponsor; + } + + public function setSponsor(Sponsor $sponsor): void + { + $this->sponsor = $sponsor; + } + + public function getAddOns(): ArrayCollection|PersistentCollection|array + { + return $this->add_ons; + } + + public function addAddOn(SummitSponsorshipAddOn $add_on): void + { + if ($this->add_ons->contains($add_on)) return; + $add_on->setSponsorship($this); + $this->add_ons->add($add_on); + } + + public function clearAddOns(): void + { + if (is_null($this->add_ons)) return; + $this->add_ons->clear(); + $this->add_ons = null; + } + + public function getType(): SummitSponsorshipType + { + return $this->type; + } + + public function setType(SummitSponsorshipType $type): void + { + $this->type = $type; + } +} \ No newline at end of file diff --git a/app/Models/Foundation/Summit/SummitSponsorshipAddOn.php b/app/Models/Foundation/Summit/SummitSponsorshipAddOn.php new file mode 100644 index 000000000..24c3544cc --- /dev/null +++ b/app/Models/Foundation/Summit/SummitSponsorshipAddOn.php @@ -0,0 +1,92 @@ +type; + } + + /** + * @throws ValidationException + */ + public function setType(string $type): void + { + if(!in_array($type, self::ValidTypes)) + throw new ValidationException(sprintf("%s is not a valid type.", $type)); + $this->type = $type; + } + + public function getName(): string + { + return $this->name; + } + + public function setName(string $name): void + { + $this->name = $name; + } + + public function getSponsorship(): SummitSponsorship + { + return $this->sponsorship; + } + + public function setSponsorship(SummitSponsorship $sponsorship): void + { + $this->sponsorship = $sponsorship; + } +} \ No newline at end of file diff --git a/app/Models/Foundation/Summit/SummitSponsorshipType.php b/app/Models/Foundation/Summit/SummitSponsorshipType.php index 017c69c3a..5cf0f87b9 100644 --- a/app/Models/Foundation/Summit/SummitSponsorshipType.php +++ b/app/Models/Foundation/Summit/SummitSponsorshipType.php @@ -173,6 +173,9 @@ class SummitSponsorshipType extends BaseEntity implements IOrderable #[ORM\Column(name: 'BadgeImageAltText', type: 'string')] private $badge_image_alt_text; + #[ORM\Column(name: 'IsPublic', type: 'boolean')] + private $is_public; + public function __construct() { $this->order = 1; @@ -182,6 +185,7 @@ public function __construct() $this->sponsor_page_use_banner_widget = true; $this->should_display_on_expo_hall_page = true; $this->should_display_on_lobby_page = true; + $this->is_public = true; } /** diff --git a/app/Repositories/Summit/DoctrineSponsorRepository.php b/app/Repositories/Summit/DoctrineSponsorRepository.php index 108e45bff..8a10d79af 100644 --- a/app/Repositories/Summit/DoctrineSponsorRepository.php +++ b/app/Repositories/Summit/DoctrineSponsorRepository.php @@ -67,8 +67,9 @@ protected function applyExtraJoins(QueryBuilder $query, ?Filter $filter = null, (!is_null($order) && $order->hasOrder("sponsorship_name")) || (!is_null($order) && $order->hasOrder("sponsorship_size")) ) - $query = $query->leftJoin("e.sponsorship", "sp") - ->leftJoin("sp.type", "st"); + $query = $query->leftJoin("e.sponsorships", "sp") + ->leftJoin("sp.type", "ssp") + ->leftJoin("ssp.type", "st"); return $query; } diff --git a/app/Services/Model/Imp/SummitSponsorService.php b/app/Services/Model/Imp/SummitSponsorService.php index fef5b2eef..874726ac3 100644 --- a/app/Services/Model/Imp/SummitSponsorService.php +++ b/app/Services/Model/Imp/SummitSponsorService.php @@ -39,6 +39,7 @@ use models\summit\SponsorSocialNetwork; use models\summit\Summit; use models\summit\SummitLeadReportSetting; +use models\summit\SummitSponsorship; use services\model\ISummitSponsorService; /** @@ -190,9 +191,6 @@ public function updateSponsor(Summit $summit, int $sponsor_id, array $payload): if (!is_null($company)) $payload['company'] = $company; - if (!is_null($sponsorship_type)) - $payload['sponsorship'] = $sponsorship_type; - $sponsor = SponsorFactory::populate($summit_sponsor, $payload); if (isset($payload['order']) && intval($payload['order']) != $sponsor->getOrder()) { diff --git a/database/migrations/model/Version20250603184703.php b/database/migrations/model/Version20250603184703.php new file mode 100644 index 000000000..f06e26977 --- /dev/null +++ b/database/migrations/model/Version20250603184703.php @@ -0,0 +1,71 @@ +hasTable(self::TableName)) { + $builder->create(self::TableName, function (Table $table) { + + $table->integer('ID', true, false); + $table->primary('ID'); + $table->timestamp('Created'); + $table->timestamp('LastEdited'); + $table->string('ClassName')->setDefault('SummitSponsorship'); + + // FK + $table->integer("SponsorID", false, false)->setNotnull(false)->setDefault('NULL'); + $table->index("SponsorID", "SponsorID"); + $table->foreign("Sponsor", "SponsorID", "ID", ["onDelete" => "CASCADE"], 'FK_Sponsor_SummitSponsorship'); + + // FK + $table->integer("TypeID", false, false)->setNotnull(false)->setDefault('NULL'); + $table->index("TypeID", "TypeID"); + $table->foreign("Summit_SponsorshipType", "TypeID", "ID", ["onDelete" => "SET NULL"], 'FK_Summit_SponsorshipType_SummitSponsorship'); + + $table->unique(['SponsorID', 'TypeID'], "SponsorID_TypeID"); + }); + } + } + + /** + * @param Schema $schema + */ + public function down(Schema $schema):void + { + $builder = new Builder($schema); + + $builder->dropIfExists(self::TableName); + } +} diff --git a/database/migrations/model/Version20250604120717.php b/database/migrations/model/Version20250604120717.php new file mode 100644 index 000000000..5d8e87dd1 --- /dev/null +++ b/database/migrations/model/Version20250604120717.php @@ -0,0 +1,65 @@ +hasTable(self::TableName)) { + $builder->create(self::TableName, function (Table $table) { + + $table->integer('ID', true, false); + $table->primary('ID'); + $table->timestamp('Created'); + $table->timestamp('LastEdited'); + $table->string('ClassName')->setDefault('SummitSponsorshipAddOn'); + $table->string('Type')->setNotnull(true)->setLength(255); + $table->string('Name')->setNotnull(false)->setLength(1024)->setDefault(null); + + // FK + $table->integer("SponsorshipID", false, false)->setNotnull(false)->setDefault('NULL'); + $table->index("SponsorshipID", "SponsorshipID"); + $table->foreign("SummitSponsorship", "SponsorshipID", "ID", ["onDelete" => "CASCADE"], 'FK_SummitSponsorship_SummitSponsorshipAddOn'); + }); + } + } + + /** + * @param Schema $schema + */ + public function down(Schema $schema):void + { + $builder = new Builder($schema); + + $builder->dropIfExists(self::TableName); + } +} diff --git a/database/migrations/model/Version20250604125852.php b/database/migrations/model/Version20250604125852.php new file mode 100644 index 000000000..20471dd7e --- /dev/null +++ b/database/migrations/model/Version20250604125852.php @@ -0,0 +1,45 @@ +addSql($sql); + } + + /** + * @param Schema $schema + */ + public function down(Schema $schema):void + { + + } +} diff --git a/database/migrations/model/Version20250604135127.php b/database/migrations/model/Version20250604135127.php new file mode 100644 index 000000000..318870db0 --- /dev/null +++ b/database/migrations/model/Version20250604135127.php @@ -0,0 +1,51 @@ +hasTable(self::TableName) && !$builder->hasColumn(self::TableName, "IsPublic")) { + $builder->table(self::TableName, function (Table $table) { + $table->boolean("IsPublic")->setNotnull(true)->setDefault(true); + }); + } + } + + /** + * @param Schema $schema + */ + public function down(Schema $schema): void + { + $builder = new Builder($schema); + if($schema->hasTable(self::TableName) && $builder->hasColumn(self::TableName, "IsPublic")) { + $builder->table(self::TableName, function (Table $table) { + $table->dropColumn("IsPublic"); + }); + } + } +} diff --git a/database/migrations/model/Version20250606154038.php b/database/migrations/model/Version20250606154038.php new file mode 100644 index 000000000..7c8ec8888 --- /dev/null +++ b/database/migrations/model/Version20250606154038.php @@ -0,0 +1,60 @@ +getDatabaseName(); + + $sql = <<addSql($sql); + + if(DBHelpers::existsFK($db_name, self::TableName, 'FK_Sponsor_SummitSponsorshipType')) { + DBHelpers::dropFK($db_name, self::TableName, 'FK_Sponsor_SummitSponsorshipType'); + } + + $builder = new Builder($schema); + if ($builder->hasTable(self::TableName) && $builder->hasColumn(self::TableName, "SummitSponsorshipTypeID")) { + $builder->table(self::TableName, function (Table $table) { + $table->dropColumn("SummitSponsorshipTypeID"); + }); + } + } + + /** + * @param Schema $schema + */ + public function down(Schema $schema): void + { + + } +} diff --git a/migrate.sh b/migrate.sh index 187cc9a5f..f31c0b04f 100755 --- a/migrate.sh +++ b/migrate.sh @@ -1,4 +1,4 @@ #!/bin/bash -php artisan doctrine:migrations:migrate --connection=config -php artisan doctrine:migrations:migrate --connection=model \ No newline at end of file +php artisan doctrine:migrations:migrate --em=config +php artisan doctrine:migrations:migrate --em=model \ No newline at end of file diff --git a/tests/InsertSummitTestData.php b/tests/InsertSummitTestData.php index 4d92f9b2c..07a962050 100644 --- a/tests/InsertSummitTestData.php +++ b/tests/InsertSummitTestData.php @@ -62,6 +62,8 @@ use models\summit\SummitOrder; use models\summit\SummitRegistrationDiscountCode; use models\summit\SummitRegistrationPromoCode; +use models\summit\SummitSponsorship; +use models\summit\SummitSponsorshipAddOn; use models\summit\SummitSponsorshipType; use models\summit\SummitTicketType; use models\summit\SummitVenue; @@ -820,7 +822,20 @@ protected static function insertSummitTestData(){ $s->setVideoLink(sprintf("https://%s.%s.video.com", $i, str_random(16))); $s->setChatLink(sprintf("https://%s.%s.chat.com", $i, str_random(16))); $s->setExternalLink(sprintf("https://%s.%s.exterma;.com", $i, str_random(16))); - $s->setSponsorship(self::$default_summit_sponsor_type); + + $sps = new SummitSponsorship(); + $sps->setType(self::$default_summit_sponsor_type); + self::$em->persist($sps); + + $s->addSponsorship($sps); + + for($j = 0; $j < 5; $j ++){ + $a = new SummitSponsorshipAddOn(); + $a->setType($j < 3 ? SummitSponsorshipAddOn::Booth_Type : SummitSponsorshipAddOn::MeetingRoom_Type); + $a->setName(sprintf("AddOn %s %s", $j, str_random(4))); + $sps->addAddOn($a); + self::$em->persist($a); + } for($j = 0; $j < 10; $j ++){ diff --git a/tests/OAuth2SummitSponsorApiTest.php b/tests/OAuth2SummitSponsorApiTest.php index e397d14bb..92f219ac5 100644 --- a/tests/OAuth2SummitSponsorApiTest.php +++ b/tests/OAuth2SummitSponsorApiTest.php @@ -133,7 +133,7 @@ public function testGetAllSponsorsBySummit(){ $params = [ 'id' => self::$summit->getId(), 'filter'=> 'company_name=@'.substr(self::$companies[0]->getName(),0,3), - 'expand' => 'company,sponsorship,sponsorship.type,extra_questions', + 'expand' => 'company,sponsorships,sponsorships.type,sponsorships.add_ons,extra_questions', 'order' => '-sponsorship_name' ]; diff --git a/tests/Unit/Entities/SummitSponsorshipTest.php b/tests/Unit/Entities/SummitSponsorshipTest.php new file mode 100644 index 000000000..562254727 --- /dev/null +++ b/tests/Unit/Entities/SummitSponsorshipTest.php @@ -0,0 +1,146 @@ +persist($sponsorship); + + // Get an existing sponsor from the test data + $sponsor = self::$sponsors[0]; + + // Set the sponsor (ManyToOne relationship) + $sponsorship->setSponsor($sponsor); + + $summit_sponsorship_type = new SummitSponsorshipType(); + $summit_sponsorship_type->setType(self::$default_sponsor_ship_type); + self::$em->persist($summit_sponsorship_type); + self::$summit->addSponsorshipType(self::$default_summit_sponsor_type); + + // Set the type (ManyToOne relationship) + $sponsorship->setType($summit_sponsorship_type); + + // Create a new add-on + $add_on = new SummitSponsorshipAddOn(); + $add_on->setName("Test Add-On " . str_random(5)); + $add_on->setType(SummitSponsorshipAddOn::Booth_Type); + self::$em->persist($add_on); + + // Add the add-on (OneToMany relationship) + $sponsorship->addAddOn($add_on); + + self::$em->flush(); + self::$em->clear(); + + // Retrieve the sponsorship from the database + $repository = self::$em->getRepository(SummitSponsorship::class); + $found_sponsorship = $repository->find($sponsorship->getId()); + + // Test ManyToOne relationship with sponsor + $found_sponsor = $found_sponsorship->getSponsor(); + $this->assertEquals($sponsor->getId(), $found_sponsor->getId()); + + // Test ManyToOne relationship with type + $found_type = $found_sponsorship->getType(); + $this->assertEquals($summit_sponsorship_type->getId(), $found_type->getId()); + + // Test OneToMany relationship with add-ons + $found_add_ons = $found_sponsorship->getAddOns()->toArray(); + $this->assertNotEmpty($found_add_ons); + $found_add_on = null; + foreach ($found_add_ons as $ao) { + if ($ao->getName() === $add_on->getName()) { + $found_add_on = $ao; + break; + } + } + $this->assertNotNull($found_add_on); + } + + public function testDeleteSummitSponsorshipChildren(){ + // Create a new sponsorship + $sponsorship = new SummitSponsorship(); + self::$em->persist($sponsorship); + + // Get an existing sponsor from the test data + $sponsor = self::$sponsors[0]; + + // Set the sponsor (ManyToOne relationship) + $sponsorship->setSponsor($sponsor); + + $summit_sponsorship_type = new SummitSponsorshipType(); + $summit_sponsorship_type->setType(self::$default_sponsor_ship_type); + self::$em->persist($summit_sponsorship_type); + self::$summit->addSponsorshipType(self::$default_summit_sponsor_type); + + // Set the type (ManyToOne relationship) + $sponsorship->setType($summit_sponsorship_type); + + // Create a new add-on + $add_on = new SummitSponsorshipAddOn(); + $add_on->setName("Test Add-On " . str_random(5)); + $add_on->setType(SummitSponsorshipAddOn::Booth_Type); + self::$em->persist($add_on); + + // Add the add-on (OneToMany relationship) + $sponsorship->addAddOn($add_on); + + self::$em->flush(); + self::$em->clear(); + + // Retrieve the sponsorship from the database + $repository = self::$em->getRepository(SummitSponsorship::class); + $found_sponsorship = $repository->find($sponsorship->getId()); + + // Clear add-ons + $found_sponsorship->clearAddOns(); + + self::$em->flush(); + self::$em->clear(); + + // Retrieve the sponsorship from the database again + $updated_sponsorship = $repository->find($sponsorship->getId()); + + // Test OneToMany relationship with add-ons + $this->assertEmpty($updated_sponsorship->getAddOns()->toArray()); + } +} \ No newline at end of file