Add Drupal application

This commit is contained in:
Oliver Davies 2019-06-05 19:35:24 +01:00
parent c00b769d67
commit 4bb39011f3
261 changed files with 22997 additions and 0 deletions

View file

@ -0,0 +1,4 @@
name: DTC Sessions Test
core: 8.x
type: module
hidden: TRUE

View file

@ -0,0 +1,20 @@
<?php
namespace Drupal\dtc_sessions_test\Factory;
use Drupal\Core\Entity\EntityInterface;
/**
* A base factory class.
*/
interface FactoryInterface {
/**
* Save and return the created entity.
*
* @return \Drupal\Core\Entity\EntityInterface
* The created entity.
*/
public function save(): EntityInterface;
}

View file

@ -0,0 +1,127 @@
<?php
namespace Drupal\dtc_sessions_test\Factory;
use Drupal\Core\Entity\EntityInterface;
use Drupal\node\Entity\Node;
use Drupal\user\UserInterface;
/**
* A factory class to create a new session.
*/
class SessionFactory implements FactoryInterface {
/**
* The node type to create.
*/
const NODE_TYPE = 'session';
/**
* The session owner.
*
* @var \Drupal\user\UserInterface
*/
private $owner;
/**
* Any additional speakers to add to the session.
*
* @var array
*/
private $extraSpeakers = [];
/**
* The session title.
*
* @var string
*/
private $sessionTitle = 'A submitted session';
/**
* The type of session.
*
* @var string
*/
private $sessionType = 'full';
/**
* Set a user to be the owner of the session.
*
* @param \Drupal\user\UserInterface $owner
* The user.
*
* @return \Drupal\dtc_sessions_test\Factory\SessionFactory
*/
public function setOwner(UserInterface $owner): self {
$this->owner = $owner;
return $this;
}
/**
* Add additional speakers to the session.
*
* @param int $count
* The number of additional speakers.
*
* @return self
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
public function addExtraSpeakers(int $count): self {
foreach (range(1, $count) as $i) {
$this->extraSpeakers[] = (new UserFactory())
->setUsername("speaker_{$i}")
->save();
}
return $this;
}
/**
* Update the session title.
*
* @param string $title
* The session title.
*
* @return $this
*/
public function setTitle(string $title): self {
$this->sessionTitle = $title;
return $this;
}
/**
* Set the event type.
*
* @param string $type
* The event type value.
*
* @return \Drupal\dtc_sessions_test\Factory\SessionFactory
*/
public function setType(string $type): self {
$this->sessionType = $type;
return $this;
}
/**
* {@inheritdoc}
*/
public function save(): EntityInterface {
$owner = $this->owner ?? (new UserFactory())->save();
$session = Node::create([
'title' => $this->sessionTitle,
'type' => self::NODE_TYPE,
'uid' => $owner,
]);
$session->set('field_session_type', $this->sessionType);
$session->set('field_speakers', array_merge([$owner], $this->extraSpeakers));
$session->save();
return $session;
}
}

View file

@ -0,0 +1,71 @@
<?php
namespace Drupal\dtc_sessions_test\Factory;
use Drupal\Core\Entity\EntityInterface;
use Drupal\user\Entity\User;
use Drupal\user\UserInterface;
/**
* A factory class to create a new user.
*/
class UserFactory implements FactoryInterface {
/**
* The user's username.
*
* @var string
*/
private $username = 'test';
/**
* The user's email address.
*
* @var string
*/
private $mail = 'test@example.com';
/**
* Set the username.
*
* @param string $name
* The username.
*
* @return \Drupal\dtc_sessions_test\Factory\UserFactory
*/
public function setUsername(string $name): self {
$this->username = $name;
return $this;
}
/**
* Set the email address.
*
* @param string $mail
* The email address.
*
* @return \Drupal\dtc_sessions_test\Factory\UserFactory
*/
public function setEmail(string $mail): self {
$this->mail = $mail;
return $this;
}
/**
* {@inheritdoc}
*/
public function save(): EntityInterface {
$user = User::create([
'name' => $this->username,
'mail' => $this->mail,
]);
$user->enforceIsNew();
$user->save();
return $user;
}
}

View file

@ -0,0 +1,106 @@
<?php
namespace Drupal\Tests\dtc_sessions\Functional;
use Drupal\Core\Url;
use Drupal\dtc_sessions_test\Factory\SessionFactory;
use Drupal\Tests\BrowserTestBase;
use Symfony\Component\HttpFoundation\Response;
/**
* Tests for submitting sessions.
*/
class SessionSubmissionTest extends BrowserTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = [
'dtc_sessions',
'dtc_sessions_test',
];
/**
* The session owner.
*
* @var \Drupal\user\UserInterface
*/
private $sessionOwner;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->sessionOwner = $this->createUser([
'create session content',
'edit own session content',
]);
}
/** @test */
public function anonymous_users_cannot_submit_sessions() {
$this->drupalGet(Url::fromRoute('node.add', ['node_type' => SessionFactory::NODE_TYPE]));
$this->assertSession()->statusCodeEquals(Response::HTTP_FORBIDDEN);
}
/** @test */
public function authenticated_users_can_submit_sessions() {
$this->drupalLogin($this->sessionOwner);
$this->drupalGet(Url::fromRoute('node.add', ['node_type' => SessionFactory::NODE_TYPE]));
$this->assertSession()->statusCodeEquals(Response::HTTP_OK);
}
/** @test */
public function authenticated_users_can_edit_their_submitted_sessions() {
$session = (new SessionFactory())
->setOwner($this->sessionOwner)
->save();
$this->drupalLogin($session->getOwner());
$this->drupalGet(Url::fromRoute('entity.node.edit_form', ['node' => $session->id()]));
$this->assertSession()->statusCodeEquals(Response::HTTP_OK);
}
/** @test */
public function authenticated_users_cannot_edit_their_users_sessions() {
$session = (new SessionFactory())
->setOwner($this->sessionOwner)
->save();
$this->drupalLogin($this->createUser());
$this->drupalGet(Url::fromRoute('entity.node.edit_form', ['node' => $session->id()]));
$this->assertSession()->statusCodeEquals(Response::HTTP_FORBIDDEN);
}
/** @test */
public function authenticated_users_cannot_delete_their_own_sessions() {
$session = (new SessionFactory())
->setOwner($this->sessionOwner)
->save();
$this->drupalLogin($session->getOwner());
$this->drupalGet(Url::fromRoute('entity.node.delete_form', ['node' => $session->id()]));
$this->assertSession()->statusCodeEquals(Response::HTTP_FORBIDDEN);
// Ensure that the node has not been deleted.
$this->drupalGet(Url::fromRoute('entity.node.edit_form', ['node' => $session->id()]));
$this->assertSession()->statusCodeEquals(Response::HTTP_OK);
}
/** @test */
public function users_can_view_their_own_session_nodes() {
/** @var \Drupal\node\NodeInterface $session */
$session = (new SessionFactory())->save();
/** @var \Drupal\Core\Session\AccountSwitcherInterface $account_switcher */
$account_switcher = $this->container->get('account_switcher');
$account_switcher->switchTo($session->getOwner());
$this->drupalGet(Url::fromRoute('entity.node.canonical', ['node' => $session->id()]));
$this->assertSession()->statusCodeEquals(Response::HTTP_OK);
}
}

View file

@ -0,0 +1,70 @@
<?php
namespace Drupal\Tests\dtc_sessions\Functional;
use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;
use Symfony\Component\HttpFoundation\Response;
/**
* Tests for submitting sessions.
*/
class SessionViewsTest extends BrowserTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = [
'block',
'dtc_sessions',
'dtc_sessions_test',
'views',
];
/**
* The session owner.
*
* @var \Drupal\user\UserInterface
*/
private $sessionOwner;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->sessionOwner = $this->createUser([
'create session content',
'edit own session content',
]);
}
/** @test */
public function a_link_to_their_sessions_is_on_the_user_page() {
$this->placeBlock('local_tasks_block');
$this->drupalLogin($this->sessionOwner);
$this->assertSession()->linkByHrefExists("user/{$this->sessionOwner->id()}/sessions");
}
/** @test */
public function users_can_view_a_list_of_their_submitted_sessions() {
$this->drupalLogin($this->sessionOwner);
$this->drupalGet(Url::fromRoute('view.user_sessions.page_1', ['user' => $this->sessionOwner->id()]));
$this->assertSession()->statusCodeEquals(Response::HTTP_OK);
}
/** @test */
public function users_cannot_view_a_list_of_other_users_submitted_sessions() {
$this->drupalLogin($this->drupalCreateUser());
$this->drupalGet(Url::fromRoute('view.user_sessions.page_1', ['user' => $this->sessionOwner->id()]));
$this->assertSession()->statusCodeEquals(Response::HTTP_FORBIDDEN);
}
}

View file

@ -0,0 +1,52 @@
<?php
namespace Drupal\Tests\dtc_sessions\Kernel;
use Drupal\Core\Test\AssertMailTrait;
use Drupal\dtc_sessions_test\Factory\SessionFactory;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\user\Entity\User;
class SessionConfirmationTest extends EntityKernelTestBase {
use AssertMailTrait;
/**
* {@inheritdoc}
*/
protected $strictConfigSchema = FALSE;
/**
* {@inheritdoc}
*/
public static $modules = [
'dtc_sessions',
'hook_event_dispatcher',
'node',
'options',
];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->installConfig([
'node',
'dtc_sessions',
]);
}
/** @test */
public function a_confirmation_email_is_sent_to_the_owner_when_a_session_is_submitted() {
$session = (new SessionFactory())->save();
$mails = $this->getMails(['id' => 'dtc_sessions_session_confirmation']);
$this->assertCount(1, $mails);
$this->assertSame($session->getOwner()->getEmail(), $mails[0]['to']);
$this->assertSame($session, $mails[0]['params']['session']);
}
}

View file

@ -0,0 +1,71 @@
<?php
namespace Drupal\Tests\dtc_sessions\Kernel;
use Drupal\dtc_sessions_test\Factory\SessionFactory;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
class SessionSubmissionTest extends EntityKernelTestBase {
/**
* {@inheritdoc}
*/
protected $strictConfigSchema = FALSE;
/**
* {@inheritdoc}
*/
public static $modules = [
'dtc_sessions',
'node',
'options',
];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->installConfig([
'node',
'dtc_sessions',
]);
}
/** @test */
public function new_sessions_are_marked_as_submitted() {
$session = (new SessionFactory())->save();
$this->assertSame('submitted', $session->get('field_session_status')->getString());
}
/** @test */
public function users_can_be_set_as_speakers() {
$session = (new SessionFactory())->save();
$speakers = $session->get('field_speakers')->getValue();
$this->assertSame($session->getOwnerId(), $speakers[0]['target_id']);
}
/** @test */
public function a_session_can_have_multiple_speakers() {
$session = (new SessionFactory())
->addExtraSpeakers(2)
->save();
$speakers = $session->get('field_speakers')->getValue();
$this->assertCount(3, $speakers);
}
/** @test */
public function a_session_has_a_type() {
$session = (new SessionFactory())
->setType('full')
->save();
$this->assertSame('full', $session->get('field_session_type')->getString());
}
}

View file

@ -0,0 +1,81 @@
<?php
namespace Drupal\Tests\dtc_sessions\Kernel;
use Drupal\dtc_sessions_test\Factory\SessionFactory;
use Drupal\dtc_sessions_test\Factory\UserFactory;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\node\Entity\Node;
use Drupal\views\ResultRow;
class SessionViewsTest extends EntityKernelTestBase {
/**
* {@inheritdoc}
*/
protected $strictConfigSchema = FALSE;
/**
* {@inheritdoc}
*/
public static $modules = [
'dtc_sessions',
'node',
'options',
'views',
];
/**
* @var \Drupal\Core\Session\AccountSwitcherInterface
*/
private $accountSwitcher;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->installConfig([
'node',
'dtc_sessions',
]);
$this->accountSwitcher = $this->container->get('account_switcher');
}
/** @test */
public function a_user_can_view_a_list_of_their_submitted_sessions() {
$user1 = (new UserFactory())->setUsername('user1')->save();
$user2 = (new UserFactory())->setUsername('user2')->save();
$session1 = (new SessionFactory())->setTitle('User 1 session')->setOwner($user1)->save();
$session2 = (new SessionFactory())->setTitle('User 2 session')->setOwner($user2)->save();
$this->accountSwitcher->switchTo($user2);
$sessions = views_get_view_result('user_sessions', 'page_1', $user2->id());
$this->assertCount(1, $sessions);
$this->assertSame($session2->uuid(), $sessions[0]->_entity->uuid());
}
/** @test */
public function sessions_are_listed_alphabetically() {
$owner = $this->createUser([], ['create session content']);
(new SessionFactory())->setOwner($owner)->setTitle('Session B')->save();
(new SessionFactory())->setOwner($owner)->setTitle('Session A')->save();
(new SessionFactory())->setOwner($owner)->setTitle('Session D')->save();
(new SessionFactory())->setOwner($owner)->setTitle('Session C')->save();
$this->accountSwitcher->switchTo($owner);
$nids = array_map(function (ResultRow $row) {
return $row->_entity->id();
}, views_get_view_result('user_sessions', 'page_1', $owner->id()));
$this->assertEquals([2, 1, 4, 3], $nids);
}
}

View file

@ -0,0 +1,27 @@
<?php
use Drupal\dtc_sessions_test\Factory\UserFactory;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\user\Entity\User;
class UserRolesTest extends EntityKernelTestBase {
/**
* {@inheritdoc}
*/
public static $modules = [
'dtc_user_roles',
'dtc_sessions',
'dtc_sessions_test',
'hook_event_dispatcher',
];
/** @test */
public function new_users_are_given_the_session_submitter_role() {
/** @var \Drupal\user\UserInterface $user */
$user = (new UserFactory())->save();
$this->assertTrue($user->hasRole('session_submitter'));
}
}