Skip to content

Commit

Permalink
Fixes, tests
Browse files Browse the repository at this point in the history
  • Loading branch information
trasher committed Aug 23, 2024
1 parent 95f7e51 commit 5b7144b
Show file tree
Hide file tree
Showing 7 changed files with 275 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ public function doEdit(Request $request, Response $response, int $id = null, str
// Validation
$valid = $subscription->check($post);
if ($valid !== true) {
$error_detected = array_merge($error_detected, $valid);
$error_detected = array_merge($error_detected, $subscription->getErrors());
}

if (count($error_detected) == 0 && isset($post['save'])) {
Expand Down
17 changes: 7 additions & 10 deletions lib/GaletteActivities/Entity/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public function __construct(Db $zdb, Login $login, int|ArrayObject $args = null)
{
$this->zdb = $zdb;
$this->login = $login;
$this->setFields();

if (is_int($args) && $args > 0) {
$this->load($args);
Expand Down Expand Up @@ -223,13 +224,9 @@ public function check(array $values): bool
Analog::ERROR
);
return false;
} else {
Analog::log(
'Activity checked successfully.',
Analog::DEBUG
);
return true;
}

return true;
}

/**
Expand All @@ -252,7 +249,7 @@ public function store(): bool

if (!isset($this->id) || $this->id == '') {
//we're inserting a new activity
$this->creation_date = date("Y-m-d H:i:s");
$this->creation_date = date("Y-m-d");
$values['creation_date'] = $this->creation_date;

$insert = $this->zdb->insert($this->getTableName());
Expand Down Expand Up @@ -349,7 +346,7 @@ public function getType(): string
*/
public function getCreationDate(bool $formatted = true): string
{
return $this->getDate('creation_date', $formatted);
return $this->getDate('creation_date', $formatted) ?? '';
}

/**
Expand Down Expand Up @@ -411,7 +408,7 @@ protected function setFields(): self
{
$this->fields = array(
self::PK => array(
'label' => _T('Activity id', 'activities'), //not a field in the form
'label' => 'Activity id', //not a field in the form
'propname' => 'id'
),
'name' => array(
Expand All @@ -435,7 +432,7 @@ protected function setFields(): self
'propname' => 'creation_date'
),
'comment' => array(
'label' => _T('Comment', 'activities'), //not a field in the form
'label' => _T('Comment', 'activities'),
'propname' => 'comment'
)
);
Expand Down
53 changes: 29 additions & 24 deletions lib/GaletteActivities/Entity/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ public function __construct(Db $zdb, Login $login, int|ArrayObject|null $args =
{
$this->zdb = $zdb;
$this->login = $login;
$this->setFields();

if (is_int($args)) {
$this->load($args);
} elseif (is_object($args)) {
Expand Down Expand Up @@ -122,10 +124,8 @@ public function load(int $id): bool
private function loadFromRS(ArrayObject $r): void
{
$this->id = (int)$r->id_subscription;
$this->id_activity = (int)$r->id_activity;
$this->activity = new Activity($this->zdb, $this->login, $this->id_activity);
$this->id_member = (int)$r->id_adh;
$this->member = new Adherent($this->zdb, $this->id_member, false);
$this->setActivity((int)$r->{Activity::PK});
$this->setMember((int)$r->{Adherent::PK});
$this->paid = (bool)$r->is_paid;
$this->payment_amount = (float)$r->payment_amount;
$this->payment_method = (int)$r->payment_method;
Expand Down Expand Up @@ -188,8 +188,7 @@ public function check(array $values): array|bool
if (!isset($values['activity']) || empty($values['activity']) || $values['activity'] == -1) {
$this->errors[] = _T('Activity is mandatory', 'activities');
} else {
$this->id_activity = (int)$values['activity'];
$this->getActivity();
$this->setActivity((int)$values['activity']);
}

//financial information
Expand All @@ -201,10 +200,10 @@ public function check(array $values): array|bool

if (isset($values['payment_amount']) && !empty($values['payment_amount'])) {
$this->payment_amount = (float)$values['payment_amount'];
}

if ($this->paid && !$this->payment_amount) {
$this->errors[] = _T('Please specify amount if subscription has been paid ;)', 'activities');
} else {
if ($this->getActivity()) {
$this->payment_amount = $this->getActivity()->getPrice();
}
}

if (isset($values['payment_method'])) {
Expand All @@ -214,21 +213,21 @@ public function check(array $values): array|bool
if (!isset($values['member']) || empty($values['member'])) {
$this->errors[] = _T('Member is mandatory', 'activities');
} else {
$this->id_member = (int)$values['member'];
$this->setMember((int)$values['member']);
}

if (isset($values['comment'])) {
$this->comment = $values['comment'];
}

if (!isset($values['subscription_date']) || empty($values['subscription_date'])) {
$this->errors[] = _T('Subscription date is mandatory!', 'activities');
$this->errors[] = _T('Subscription date is mandatory', 'activities');
} else {
$this->setDate('subscription_date', $values['subscription_date']);
}

if (!isset($values['end_date']) || empty($values['end_date'])) {
$this->errors[] = _T('End date is mandatory!', 'activities');
$this->errors[] = _T('End date is mandatory', 'activities');
} else {
$this->setDate('end_date', $values['end_date']);
}
Expand All @@ -239,14 +238,10 @@ public function check(array $values): array|bool
print_r($this->errors, true),
Analog::ERROR
);
return $this->errors;
} else {
Analog::log(
'Subscription checked successfully.',
Analog::DEBUG
);
return true;
return false;
}

return true;
}

/**
Expand Down Expand Up @@ -274,7 +269,7 @@ public function store(): bool

if (!isset($this->id) || $this->id == '') {
//we're inserting a new subscription
$this->creation_date = date("Y-m-d H:i:s");
$this->creation_date = date("Y-m-d");
$values['creation_date'] = $this->creation_date;

$insert = $this->zdb->insert($this->getTableName());
Expand Down Expand Up @@ -439,7 +434,7 @@ public function getPaymentMethodName(): string
*/
public function getCreationDate(bool $formatted = true): string
{
return $this->getDate('creation_date', $formatted);
return $this->getDate('creation_date', $formatted) ?? '';
}

/**
Expand Down Expand Up @@ -537,7 +532,7 @@ protected function setFields(): self
{
$this->fields = array(
self::PK => array(
'label' => _T('Subscription id', 'activities'), //not a field in the form
'label' => 'Subscription id', //not a field in the form
'propname' => 'id'
),
Activity::PK => array(
Expand Down Expand Up @@ -573,11 +568,21 @@ protected function setFields(): self
'propname' => 'end_date'
),
'comment' => array(
'label' => _T('Comment', 'activities'), //not a field in the form
'label' => _T('Comment', 'activities'),
'propname' => 'comment'
)
);

return $this;
}

/**
* Get errors
*
* @return array<string>
*/
public function getErrors(): array
{
return $this->errors;
}
}
5 changes: 1 addition & 4 deletions templates/default/activities.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@

{% block infoline %}
{% set infoline = {
'label': _Tn("%1$s activity", "%1$s activities", nb, "activities")|replace({"%1$s": nb}),
'route': {
'name': 'filter-activitieslist'
}
'label': _Tn("%1$s activity", "%1$s activities", nb, "activities")|replace({"%1$s": nb})
} %}
{{ parent() }}
{% endblock %}
Expand Down
10 changes: 5 additions & 5 deletions templates/default/activity.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,19 @@
{% include "components/forms/text.html.twig" with {
id: 'name',
value: activity.getName(),
label: _T("Name", "activities"),
label: activity.getFieldLabel('name')
} %}

{% include "components/forms/text.html.twig" with {
id: 'type',
value: activity.getType(),
label: _T("Type", "activities"),
label: activity.getFieldLabel('type')
} %}

{% include "components/forms/text.html.twig" with {
id: 'price',
value: activity.getPrice(),
label: _T("Price", "activities"),
label: activity.getFieldLabel('price')
} %}

{% set group_list_values = {(0): _T("Select a group")} %}
Expand All @@ -59,15 +59,15 @@
id: 'id_group',
value: group_id,
values: group_list_values,
label: _T("Attach to group", "events"),
label: _T("Attach to group", "activitie"),
title: _T("Group to attach members that subscribes to activity.", "activities"),
raw_labels: true
} %}

{% include "components/forms/textarea.html.twig" with {
id: 'comment',
value: activity.getComment(),
label: _T("Comment", "activities"),
label: activity.getFieldLabel('comment'),
} %}
</div>
</div>
Expand Down
14 changes: 13 additions & 1 deletion tests/GaletteActivities/Entity/tests/units/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,20 @@ public function testCrud(): void
$this->assertFalse($activity->check($data));
$this->assertSame(['Name is mandatory'], $activity->getErrors());

//required activity name
$data = [
'name' => 'Test activity',
'comment' => 'Test comment',
'type' => '1234',
];
$this->assertFalse($activity->check($data));
$this->assertSame(['Type is too long'], $activity->getErrors());

//add new activity
$data = [
'name' => 'Test activity',
'comment' => 'Test comment',
'type' => 'one'
];
$this->assertTrue($activity->check($data));
$this->assertTrue($activity->store());
Expand All @@ -99,9 +109,9 @@ public function testCrud(): void
$this->assertTrue($activity->load($first_id));
$this->assertSame('Test activity', $activity->getName());
$this->assertSame('Test comment', $activity->getComment());
$this->assertSame('one', $activity->getType());
//FIXME: lang must be changed to have a different date format
$this->assertNotSame('', $activity->getCreationDate());
$this->assertNotSame('', $activity->getCreationDate(false));
$this->assertNull($activity->getPrice());
$this->assertNull($activity->getGroup());

Expand All @@ -115,12 +125,14 @@ public function testCrud(): void
//edit activity
$data['name'] = 'Test activity edited';
$data['price'] = 10.5;
$data['comment'] = '';
$this->assertTrue($activity->check($data));
$this->assertTrue($activity->store());
$this->assertTrue($activity->load($first_id));

$this->assertSame('Test activity edited', $activity->getName());
$this->assertSame(10.5, $activity->getPrice());
$this->assertSame('', $activity->getComment());

$group = new \Galette\Entity\Group();
$group->setName('Test group' . $this->seed);
Expand Down
Loading

0 comments on commit 5b7144b

Please sign in to comment.