Skip to content

Commit 816da6e

Browse files
committed
Updating tests to cover latest changes in views, fixing bugs.
1 parent f620013 commit 816da6e

File tree

11 files changed

+130
-62
lines changed

11 files changed

+130
-62
lines changed

app/model/entity/Assignment.php

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,20 @@ class Assignment extends AssignmentBase implements IExercise
2323
{
2424
use ExerciseData;
2525

26+
/**
27+
* @ORM\OneToMany(targetEntity="ExerciseFileLink", mappedBy="assignment", cascade={"persist"})
28+
* @var Collection<ExerciseFileLink>
29+
*/
30+
protected $fileLinks;
31+
32+
/**
33+
* @return Collection<ExerciseFileLink>
34+
*/
35+
public function getFileLinks(): Collection
36+
{
37+
return $this->fileLinks;
38+
}
39+
2640
private function __construct(
2741
DateTime $firstDeadline,
2842
int $maxPointsBeforeFirstDeadline,
@@ -79,12 +93,6 @@ private function __construct(
7993
$this->fileLinks = new ArrayCollection();
8094
$this->solutionFilesLimit = $exercise->getSolutionFilesLimit();
8195
$this->solutionSizeLimit = $exercise->getSolutionSizeLimit();
82-
83-
// copy file links from exercise
84-
foreach ($exercise->getFileLinks() as $link) {
85-
$newLink = ExerciseFileLink::copyForAssignment($link, $this);
86-
$this->fileLinks->add($newLink);
87-
}
8896
}
8997

9098
public static function assignToGroup(
@@ -113,6 +121,12 @@ public static function assignToGroup(
113121

114122
$group->addAssignment($assignment);
115123

124+
// copy file links from exercise
125+
foreach ($exercise->getFileLinks() as $link) {
126+
$newLink = ExerciseFileLink::copyForAssignment($link, $assignment);
127+
$assignment->fileLinks->add($newLink);
128+
}
129+
116130
return $assignment;
117131
}
118132

@@ -620,6 +634,9 @@ public function setSubmissionsCountLimit(int $submissionsCountLimit): void
620634
$this->submissionsCountLimit = $submissionsCountLimit;
621635
}
622636

637+
/**
638+
* @return Collection<AssignmentSolution>
639+
*/
623640
public function getAssignmentSolutions(): Collection
624641
{
625642
return $this->assignmentSolutions;
@@ -755,7 +772,7 @@ public function getPlagiarismBatch(): ?PlagiarismDetectionBatch
755772
return $this->plagiarismBatch;
756773
}
757774

758-
public function setPlagiarismBatch(?PlagiarismDetectionBatch $batch = null)
775+
public function setPlagiarismBatch(?PlagiarismDetectionBatch $batch = null): void
759776
{
760777
$this->plagiarismBatch = $batch;
761778
}

app/model/entity/AssignmentSolution.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,9 @@ public function setOverriddenPoints(?int $overriddenPoints): void
343343
$this->overriddenPoints = $overriddenPoints;
344344
}
345345

346+
/**
347+
* @return Collection<AssignmentSolutionSubmission>
348+
*/
346349
public function getSubmissions(): Collection
347350
{
348351
return $this->submissions;

app/model/entity/Exercise.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,20 @@ class Exercise implements IExercise
111111
*/
112112
protected $archivedAt = null;
113113

114+
/**
115+
* @ORM\OneToMany(targetEntity="ExerciseFileLink", mappedBy="exercise", cascade={"persist"})
116+
* @var Collection<ExerciseFileLink>
117+
*/
118+
protected $fileLinks;
119+
120+
/**
121+
* @return Collection<ExerciseFileLink>
122+
*/
123+
public function getFileLinks(): Collection
124+
{
125+
return $this->fileLinks;
126+
}
127+
114128
/**
115129
* Constructor
116130
* @param int $version
@@ -390,6 +404,9 @@ public function setAuthor(User $author): void
390404
$this->author = $author;
391405
}
392406

407+
/**
408+
* @return ReadableCollection<User>
409+
*/
393410
public function getAdmins(): ReadableCollection
394411
{
395412
return $this->admins->filter(

app/model/entity/ExerciseFileLink.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class ExerciseFileLink implements JsonSerializable
5959
protected $exercise;
6060

6161
/**
62-
* @ORM\ManyToOne(targetEntity="Assignment", inversedBy="fileLinks")
62+
* @ORM\ManyToOne(targetEntity="Assignment")
6363
* @ORM\JoinColumn(onDelete="CASCADE")
6464
*/
6565
protected $assignment;

app/model/entity/Group.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ public function removeMembership(GroupMembership $membership): bool
474474
/**
475475
* Get all members of the group of given type
476476
* @param string[] ...$types
477-
* @return ReadableCollection
477+
* @return ReadableCollection<User>
478478
*/
479479
public function getMembers(...$types): ReadableCollection
480480
{
@@ -505,12 +505,18 @@ function (GroupMembership $membership) {
505505
)->getValues();
506506
}
507507

508-
public function getStudents()
508+
/**
509+
* @return ReadableCollection<User>
510+
*/
511+
public function getStudents(): ReadableCollection
509512
{
510513
return $this->getMembers(GroupMembership::TYPE_STUDENT);
511514
}
512515

513-
public function getStudentsIds()
516+
/**
517+
* @return string[] ids
518+
*/
519+
public function getStudentsIds(): array
514520
{
515521
return $this->getMembersIds(GroupMembership::TYPE_STUDENT);
516522
}

app/model/entity/Solution.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function __construct(User $author, RuntimeEnvironment $runtimeEnvironment
7474
$this->subdir = $this->createdAt->format('Y-m');
7575
}
7676

77-
public function addFile(SolutionFile $file)
77+
public function addFile(SolutionFile $file): void
7878
{
7979
$this->files->add($file);
8080
}
@@ -97,7 +97,7 @@ public function getSolutionParams(): SolutionParams
9797
return new SolutionParams(Yaml::parse($this->solutionParams));
9898
}
9999

100-
public function setSolutionParams(SolutionParams $params)
100+
public function setSolutionParams(SolutionParams $params): void
101101
{
102102
$dumped = Yaml::dump($params->toArray());
103103
$this->solutionParams = $dumped ?: "";
@@ -137,6 +137,9 @@ public function getAuthorId(): ?string
137137
return $this->author->isDeleted() ? null : $this->author->getId();
138138
}
139139

140+
/**
141+
* @return Collection<SolutionFile>
142+
*/
140143
public function getFiles(): Collection
141144
{
142145
return $this->files;

app/model/entity/base/ExerciseData.php

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -364,30 +364,6 @@ function (AttachmentFile $file) {
364364
)->getValues();
365365
}
366366

367-
/**
368-
* @ORM\OneToMany(targetEntity="ExerciseFileLink", mappedBy="exerciseFile", cascade={"persist"})
369-
* @var Collection<ExerciseFileLink>
370-
*/
371-
protected $fileLinks;
372-
373-
/**
374-
* @return Collection<ExerciseFileLink>
375-
*/
376-
public function getFileLinks(): Collection
377-
{
378-
return $this->fileLinks;
379-
}
380-
381-
public function addFileLink(ExerciseFileLink $fileLink): void
382-
{
383-
$this->fileLinks->add($fileLink);
384-
}
385-
386-
public function removeFileLink(ExerciseFileLink $fileLink): bool
387-
{
388-
return $this->fileLinks->removeElement($fileLink);
389-
}
390-
391367
/**
392368
* @ORM\Column(type="integer", nullable=true)
393369
* How many files may one submit in a solution.

fixtures/demo/25-exercises.neon

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,31 @@ App\Model\Entity\ExerciseFile:
216216
- "shahash"
217217
- @demoAdmin
218218
- @demoExercise
219+
linkedExerciseFile:
220+
__construct:
221+
- "lib.py"
222+
- "<dateTimeBetween('-3 years', '-1 years')>"
223+
- 42
224+
- "hashmash"
225+
- @demoAdmin
226+
- @demoExercise
227+
228+
App\Model\Entity\ExerciseFileLink:
229+
demoExerciseFileLink1:
230+
__construct:
231+
createForExercise:
232+
- "LIB"
233+
- @linkedExerciseFile
234+
- @demoExercise
235+
- "student"
236+
- "thelib.py"
237+
238+
demoExerciseFileLink2:
239+
__construct:
240+
createForExercise:
241+
- "ORIG"
242+
- @linkedExerciseFile
243+
- @demoExercise
219244

220245
App\Model\Entity\ExerciseTag:
221246
demoTag1:

tests/Presenters/AssignmentsPresenter.phpt

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class TestAssignmentsPresenter extends Tester\TestCase
7676
$this->hardwareGroups = $container->getByType(HardwareGroups::class);
7777
$this->assignmentSolutionViewFactory = $container->getByType(AssignmentSolutionViewFactory::class);
7878

79-
// patch container, since we cannot create actual file storage manarer
79+
// patch container, since we cannot create actual file storage manager
8080
$fsName = current($this->container->findByType(FileStorageManager::class));
8181
$this->container->removeService($fsName);
8282
$this->container->addService($fsName, new FileStorageManager(
@@ -234,8 +234,7 @@ class TestAssignmentsPresenter extends Tester\TestCase
234234

235235
/** @var Mockery\Mock | AssignmentEmailsSender $mockAssignmentEmailsSender */
236236
$mockAssignmentEmailsSender = Mockery::mock(JobConfig\JobConfig::class);
237-
$mockAssignmentEmailsSender->shouldReceive()->never(
238-
); // this is the main assertion of this test (no mail is sent)
237+
$mockAssignmentEmailsSender->shouldReceive()->never(); // this is the main assertion of this test (no mail is sent)
239238
$this->presenter->assignmentEmailsSender = $mockAssignmentEmailsSender;
240239

241240
$mockEvaluations = Mockery::mock(SolutionEvaluations::class);
@@ -619,32 +618,39 @@ class TestAssignmentsPresenter extends Tester\TestCase
619618
{
620619
PresenterTestHelper::loginDefaultAdmin($this->container);
621620

621+
$exercises = array_filter(
622+
$this->presenter->exercises->findAll(),
623+
function (Exercise $e) {
624+
return !$e->getFileLinks()->isEmpty(); // select the exercise with file links
625+
}
626+
);
627+
Assert::count(1, $exercises);
622628
/** @var Exercise $exercise */
623-
$exercise = $this->presenter->exercises->findAll()[0];
629+
$exercise = array_pop($exercises);
630+
624631
/** @var Group $group */
625632
$group = $this->presenter->groups->findAll()[0];
626633

627-
$request = new Nette\Application\Request(
634+
$payload = PresenterTestHelper::performPresenterRequest(
635+
$this->presenter,
628636
'V1:Assignments',
629637
'POST',
630638
['action' => 'create'],
631639
['exerciseId' => $exercise->getId(), 'groupId' => $group->getId()]
632640
);
633641

634-
$response = $this->presenter->run($request);
635-
Assert::type(Nette\Application\Responses\JsonResponse::class, $response);
636-
637-
$result = $response->getPayload();
638-
Assert::equal(200, $result['code']);
639-
640642
/** @var AssignmentViewFactory $viewFactory */
641643
$viewFactory = $this->container->getByType(AssignmentViewFactory::class);
642644

643645
// Make sure the assignment was persisted
644646
Assert::same(
645-
$viewFactory->getAssignment($this->presenter->assignments->findOneBy(['id' => $result['payload']["id"]])),
646-
$result['payload']
647+
$viewFactory->getAssignment($this->presenter->assignments->findOneBy(['id' => $payload["id"]])),
648+
$payload
647649
);
650+
Assert::count(2, $payload['localizedTextsLinks']);
651+
$keys = array_keys($payload['localizedTextsLinks']);
652+
sort($keys);
653+
Assert::same(['LIB', 'ORIG'], $keys);
648654
}
649655

650656
public function testCreateAssignmentFromLockedExercise()

tests/Presenters/ExerciseFilesPresenter.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class TestExerciseFilesPresenter extends Tester\TestCase
5757
$this->exercises = $container->getByType(App\Model\Repository\Exercises::class);
5858
$this->attachmentFiles = $container->getByType(\App\Model\Repository\AttachmentFiles::class);
5959

60-
// patch container, since we cannot create actual file storage manarer
60+
// patch container, since we cannot create actual file storage manager
6161
$fsName = current($this->container->findByType(FileStorageManager::class));
6262
$this->container->removeService($fsName);
6363
$this->container->addService($fsName, new FileStorageManager(

0 commit comments

Comments
 (0)