OpignoILTTest.php in Opigno Instructor-led Trainings 8
File
tests/src/Functional/OpignoILTTest.php
View source
<?php
namespace Drupal\Tests\opigno_ilt\Functional;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Url;
use Drupal\opigno_ilt\Entity\ILT;
class OpignoILTTest extends OpignoILTBrowserTestBase {
public function testOpignoILTAccess() {
$training = $this
->createGroup();
$student_1 = $this
->drupalCreateUser();
$student_2 = $this
->drupalCreateUser();
$training
->addMember($student_1);
$training
->addMember($student_2);
$training
->save();
$this
->assertEquals(3, count($training
->getMembers()), 'Training members - a Group admin and two students.');
$meeting = ILT::create([
'title' => $this
->randomString(),
'training' => $training
->id(),
'place' => $this
->randomString(),
'date' => $this
->createDummyDaterange(),
]);
$meeting
->save();
$ilt_path = Url::fromRoute('entity.opigno_ilt.canonical', [
'opigno_ilt' => $meeting
->id(),
]);
$this
->drupalLogout();
$this
->drupalLogin($student_1);
$this
->drupalGet($ilt_path);
$this
->assertSession()
->pageTextContains($meeting
->getTitle());
$this
->assertSession()
->statusCodeEquals(200, 'Student without restriction has access to ILT');
$meeting = ILT::load($meeting
->id());
$meeting
->setMembersIds([
$student_2
->id(),
]);
$meeting
->save();
$this
->drupalGet($ilt_path);
$this
->assertSession()
->pageTextNotContains($meeting
->getTitle());
$this
->assertSession()
->statusCodeEquals(403, 'Student with restriction does not have access to ILT');
}
protected function createDummyDateRange() {
$display_format = 'm-d-Y H:i:s';
$start_date = date($display_format, strtotime("1 hour"));
$end_date = date($display_format, strtotime("2 hour"));
$start_date_value = DrupalDateTime::createFromFormat($display_format, $start_date);
$end_date_value = DrupalDateTime::createFromFormat($display_format, $end_date);
$date_range = [
'value' => $start_date_value
->setTimezone(new \DateTimeZone(date_default_timezone_get()))
->format(DrupalDateTime::FORMAT),
'end_value' => $end_date_value
->setTimezone(new \DateTimeZone(date_default_timezone_get()))
->format(DrupalDateTime::FORMAT),
];
return $date_range;
}
}