Move into nested docroot

This commit is contained in:
Rob Davies 2017-02-13 15:31:17 +00:00
parent 83a0d3a149
commit c8b70abde9
13405 changed files with 0 additions and 0 deletions

View file

@ -0,0 +1,71 @@
<?php
namespace Drupal\Tests\contact\Kernel;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
/**
* Tests the message entity class.
*
* @group contact
* @see \Drupal\contact\Entity\Message
*/
class MessageEntityTest extends EntityKernelTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array(
'system',
'contact',
'field',
'user',
'contact_test',
);
protected function setUp() {
parent::setUp();
$this->installConfig(array('contact', 'contact_test'));
}
/**
* Test some of the methods.
*/
public function testMessageMethods() {
$message_storage = $this->container->get('entity.manager')->getStorage('contact_message');
$message = $message_storage->create(array('contact_form' => 'feedback'));
// Check for empty values first.
$this->assertEqual($message->getMessage(), '');
$this->assertEqual($message->getSenderName(), '');
$this->assertEqual($message->getSenderMail(), '');
$this->assertFalse($message->copySender());
// Check for default values.
$this->assertEqual('feedback', $message->getContactForm()->id());
$this->assertFalse($message->isPersonal());
// Set some values and check for them afterwards.
$message->setMessage('welcome_message');
$message->setSenderName('sender_name');
$message->setSenderMail('sender_mail');
$message->setCopySender(TRUE);
$this->assertEqual($message->getMessage(), 'welcome_message');
$this->assertEqual($message->getSenderName(), 'sender_name');
$this->assertEqual($message->getSenderMail(), 'sender_mail');
$this->assertTrue($message->copySender());
$no_access_user = $this->createUser(['uid' => 2]);
$access_user = $this->createUser(['uid' => 3], ['access site-wide contact form']);
$admin = $this->createUser(['uid' => 4], ['administer contact forms']);
$this->assertFalse(\Drupal::entityManager()->getAccessControlHandler('contact_message')->createAccess(NULL, $no_access_user));
$this->assertTrue(\Drupal::entityManager()->getAccessControlHandler('contact_message')->createAccess(NULL, $access_user));
$this->assertTrue($message->access('edit', $admin));
$this->assertFalse($message->access('edit', $access_user));
}
}

View file

@ -0,0 +1,64 @@
<?php
namespace Drupal\Tests\contact\Kernel\Migrate;
use Drupal\contact\Entity\ContactForm;
use Drupal\contact\ContactFormInterface;
use Drupal\Tests\migrate_drupal\Kernel\d6\MigrateDrupal6TestBase;
/**
* Migrate contact categories to contact.form.*.yml.
*
* @group contact_category
*/
class MigrateContactCategoryTest extends MigrateDrupal6TestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('contact');
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->executeMigration('contact_category');
}
/**
* Performs various assertions on a single contact form entity.
*
* @param string $id
* The contact form ID.
* @param string $expected_label
* The expected label.
* @param string[] $expected_recipients
* The recipient e-mail addresses the form should have.
* @param string $expected_reply
* The expected reply message.
* @param int $expected_weight
* The contact form's expected weight.
*/
protected function assertEntity($id, $expected_label, array $expected_recipients, $expected_reply, $expected_weight) {
/** @var \Drupal\contact\ContactFormInterface $entity */
$entity = ContactForm::load($id);
$this->assertTrue($entity instanceof ContactFormInterface);
$this->assertIdentical($expected_label, $entity->label());
$this->assertIdentical($expected_recipients, $entity->getRecipients());
$this->assertIdentical($expected_reply, $entity->getReply());
$this->assertIdentical($expected_weight, $entity->getWeight());
}
/**
* The Drupal 6 and 7 contact categories to Drupal 8 migration.
*/
public function testContactCategory() {
$this->assertEntity('website_feedback', 'Website feedback', ['admin@example.com'], '', 0);
$this->assertEntity('some_other_category', 'Some other category', ['test@example.com'], 'Thanks for contacting us, we will reply ASAP!', 1);
$this->assertEntity('a_category_much_longer_than_thir', 'A category much longer than thirty two characters', ['fortyninechars@example.com'], '', 2);
}
}

View file

@ -0,0 +1,75 @@
<?php
namespace Drupal\Tests\contact\Kernel\Migrate\d6;
use Drupal\contact\Entity\ContactForm;
use Drupal\Tests\migrate_drupal\Kernel\d6\MigrateDrupal6TestBase;
/**
* Migrate contact categories to contact.form.*.yml.
*
* @group migrate_drupal_6
*/
class MigrateContactCategoryTest extends MigrateDrupal6TestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['contact'];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->executeMigration('contact_category');
}
/**
* The Drupal 6 contact categories to Drupal 8 migration.
*/
public function testContactCategory() {
/** @var \Drupal\contact\Entity\ContactForm $contact_form */
$contact_form = ContactForm::load('website_feedback');
$this->assertIdentical('Website feedback', $contact_form->label());
$this->assertIdentical(array('admin@example.com'), $contact_form->getRecipients());
$this->assertIdentical('', $contact_form->getReply());
$this->assertIdentical(0, $contact_form->getWeight());
$contact_form = ContactForm::load('some_other_category');
$this->assertIdentical('Some other category', $contact_form->label());
$this->assertIdentical(array('test@example.com'), $contact_form->getRecipients());
$this->assertIdentical('Thanks for contacting us, we will reply ASAP!', $contact_form->getReply());
$this->assertIdentical(1, $contact_form->getWeight());
$contact_form = ContactForm::load('a_category_much_longer_than_thir');
$this->assertIdentical('A category much longer than thirty two characters', $contact_form->label());
$this->assertIdentical(array('fortyninechars@example.com'), $contact_form->getRecipients());
$this->assertIdentical('', $contact_form->getReply());
$this->assertIdentical(2, $contact_form->getWeight());
// Test there are no duplicated roles.
$contact_forms = [
'website_feedback1',
'some_other_category1',
'a_category_much_longer_than_thir1',
];
$this->assertEmpty(ContactForm::loadMultiple($contact_forms));
/*
* Remove the map row for the Website feedback contact form so that it
* can be migrated again.
*/
$id_map = $this->getMigration('contact_category')->getIdMap();
$id_map->delete(['cid' => '1']);
$this->executeMigration('contact_category');
// Test there is a duplicate Website feedback form.
$contact_form = ContactForm::load('website_feedback1');
$this->assertSame('Website feedback', $contact_form->label());
$this->assertSame(array('admin@example.com'), $contact_form->getRecipients());
$this->assertSame('', $contact_form->getReply());
$this->assertSame(0, $contact_form->getWeight());
}
}

View file

@ -0,0 +1,41 @@
<?php
namespace Drupal\Tests\contact\Kernel\Migrate\d6;
use Drupal\config\Tests\SchemaCheckTestTrait;
use Drupal\Tests\migrate_drupal\Kernel\d6\MigrateDrupal6TestBase;
/**
* Upgrade variables to contact.settings.yml.
*
* @group migrate_drupal_6
*/
class MigrateContactSettingsTest extends MigrateDrupal6TestBase {
use SchemaCheckTestTrait;
/**
* {@inheritdoc}
*/
public static $modules = ['contact'];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->executeMigrations(['contact_category', 'd6_contact_settings']);
}
/**
* Tests migration of contact variables to contact.settings.yml.
*/
public function testContactSettings() {
$config = $this->config('contact.settings');
$this->assertIdentical(TRUE, $config->get('user_default_enabled'));
$this->assertIdentical(3, $config->get('flood.limit'));
$this->assertIdentical('some_other_category', $config->get('default_form'));
$this->assertConfigSchema(\Drupal::service('config.typed'), 'contact.settings', $config->get());
}
}

View file

@ -0,0 +1,38 @@
<?php
namespace Drupal\Tests\contact\Kernel\Migrate\d7;
use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;
/**
* Tests migration of Contact settings to configuration.
*
* @group migrate_drupal_7
*/
class MigrateContactSettingsTest extends MigrateDrupal7TestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['contact'];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->executeMigration('contact_category');
$this->executeMigration('d7_contact_settings');
}
/**
* Tests migration of Contact's variables to configuration.
*/
public function testContactSettings() {
$config = $this->config('contact.settings');
$this->assertTrue($config->get('user_default_enabled'));
$this->assertIdentical(33, $config->get('flood.limit'));
$this->assertEqual('website_testing', $config->get('default_form'));
}
}

View file

@ -0,0 +1,57 @@
<?php
namespace Drupal\Tests\contact\Kernel\Plugin\migrate\source;
use Drupal\Tests\migrate\Kernel\MigrateSqlSourceTestBase;
/**
* Tests D6 contact category source plugin.
*
* @covers \Drupal\contact\Plugin\migrate\source\ContactCategory
* @group contact
*/
class ContactCategoryTest extends MigrateSqlSourceTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['contact', 'migrate_drupal', 'user'];
/**
* {@inheritdoc}
*/
public function providerSource() {
$tests = [
[
'source_data' => [],
'expected_data' => [],
],
];
$tests[0]['expected_data'] = [
[
'cid' => 1,
'category' => 'contact category value 1',
'recipients' => ['admin@example.com', 'user@example.com'],
'reply' => 'auto reply value 1',
'weight' => 0,
'selected' => 0,
],
[
'cid' => 2,
'category' => 'contact category value 2',
'recipients' => ['admin@example.com', 'user@example.com'],
'reply' => 'auto reply value 2',
'weight' => 0,
'selected' => 0,
],
];
foreach ($tests[0]['expected_data'] as $k => $row) {
$row['recipients'] = implode(',', $row['recipients']);
$tests[0]['source_data']['contact'][$k] = $row;
}
return $tests;
}
}

View file

@ -0,0 +1,54 @@
<?php
namespace Drupal\Tests\contact\Kernel\Plugin\migrate\source\d6;
use Drupal\Tests\migrate\Kernel\MigrateSqlSourceTestBase;
/**
* Tests D6 contact settings source plugin.
*
* @covers \Drupal\contact\Plugin\migrate\source\ContactSettings
* @group contact
*/
class ContactSettingsTest extends MigrateSqlSourceTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['contact', 'migrate_drupal', 'user'];
/**
* {@inheritdoc}
*/
public function providerSource() {
$tests = [];
$tests[0]['source_data']['variable'] = [
[
'name' => 'site_name',
'value' => serialize('Blorf!'),
],
];
$tests[0]['source_data']['contact'] = [
[
'cid' => '1',
'category' => 'Website feedback',
'recipients' => 'admin@example.com',
'reply' => '',
'weight' => '0',
'selected' => '1',
]
];
$tests[0]['expected_data'] = [
[
'default_category' => '1',
'site_name' => 'Blorf!',
],
];
$tests[0]['expected_count'] = NULL;
$tests[0]['configuration']['variables'] = ['site_name'];
return $tests;
}
}

View file

@ -0,0 +1,409 @@
<?php
namespace Drupal\Tests\contact\Unit;
use Drupal\contact\MailHandler;
use Drupal\contact\MessageInterface;
use Drupal\Core\Language\Language;
use Drupal\Core\Session\AccountInterface;
use Drupal\Tests\UnitTestCase;
/**
* @coversDefaultClass \Drupal\contact\MailHandler
* @group contact
*/
class MailHandlerTest extends UnitTestCase {
/**
* Language manager service.
*
* @var \Drupal\Core\Language\LanguageManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $languageManager;
/**
* Logger service.
*
* @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $logger;
/**
* Mail manager service.
*
* @var \Drupal\Core\Mail\MailManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $mailManager;
/**
* Contact mail messages service.
*
* @var \Drupal\contact\MailHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $contactMailHandler;
/**
* The contact form entity.
*
* @var \Drupal\contact\ContactFormInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $contactForm;
/**
* The entity manager service.
*
* @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $entityManager;
/**
* The user storage handler.
*
* @var \Drupal\Core\Entity\EntityStorageInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $userStorage;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->mailManager = $this->getMock('\Drupal\Core\Mail\MailManagerInterface');
$this->languageManager = $this->getMock('\Drupal\Core\Language\LanguageManagerInterface');
$this->logger = $this->getMock('\Psr\Log\LoggerInterface');
$this->entityManager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface');
$this->userStorage = $this->getMock('\Drupal\Core\Entity\EntityStorageInterface');
$this->entityManager->expects($this->any())
->method('getStorage')
->with('user')
->willReturn($this->userStorage);
$string_translation = $this->getStringTranslationStub();
$this->contactMailHandler = new MailHandler($this->mailManager, $this->languageManager, $this->logger, $string_translation, $this->entityManager);
$language = new Language(array('id' => 'en'));
$this->languageManager->expects($this->any())
->method('getDefaultLanguage')
->will($this->returnValue($language));
$this->languageManager->expects($this->any())
->method('getCurrentLanguage')
->will($this->returnValue($language));
}
/**
* Tests the children() method with an invalid key.
*
* @expectedException \Drupal\contact\MailHandlerException
* @expectedExceptionMessage Unable to determine message recipient
*
* @covers ::sendMailMessages
*/
public function testInvalidRecipient() {
$message = $this->getMock('\Drupal\contact\MessageInterface');
$message->expects($this->once())
->method('isPersonal')
->willReturn(TRUE);
$message->expects($this->once())
->method('getPersonalRecipient')
->willReturn(NULL);
$message->expects($this->once())
->method('getContactForm')
->willReturn($this->getMock('\Drupal\contact\ContactFormInterface'));
$sender = $this->getMock('\Drupal\Core\Session\AccountInterface');
$this->userStorage->expects($this->any())
->method('load')
->willReturn($sender);
// User IDs 1 and 0 have special implications, use 3 instead.
$sender->expects($this->any())
->method('id')
->willReturn(3);
$sender->expects($this->once())
->method('isAnonymous')
->willReturn(FALSE);
$this->contactMailHandler->sendMailMessages($message, $sender);
}
/**
* Tests the sendMailMessages method.
*
* @dataProvider getSendMailMessages
*
* @covers ::sendMailMessages
*/
public function testSendMailMessages(MessageInterface $message, AccountInterface $sender, $results) {
$this->logger->expects($this->once())
->method('notice');
$this->mailManager->expects($this->any())
->method('mail')
->willReturnCallback(
function($module, $key, $to, $langcode, $params, $from) use (&$results) {
$result = array_shift($results);
$this->assertEquals($module, $result['module']);
$this->assertEquals($key, $result['key']);
$this->assertEquals($to, $result['to']);
$this->assertEquals($langcode, $result['langcode']);
$this->assertArrayEquals($params, $result['params']);
$this->assertEquals($from, $result['from']);
});
$this->userStorage->expects($this->any())
->method('load')
->willReturn(clone $sender);
$this->contactMailHandler->sendMailMessages($message, $sender);
}
/**
* Data provider for ::testSendMailMessages.
*/
public function getSendMailMessages() {
$data = array();
$recipients = array('admin@drupal.org', 'user@drupal.org');
$default_result = array(
'module' => 'contact',
'key' => '',
'to' => implode(', ', $recipients),
'langcode' => 'en',
'params' => array(),
'from' => 'anonymous@drupal.org',
);
$results = array();
$message = $this->getAnonymousMockMessage($recipients, '');
$sender = $this->getMockSender();
$result = array(
'key' => 'page_mail',
'params' => array(
'contact_message' => $message,
'sender' => $sender,
'contact_form' => $message->getContactForm(),
),
);
$results[] = $result + $default_result;
$data[] = array($message, $sender, $results);
$results = array();
$message = $this->getAnonymousMockMessage($recipients, 'reply');
$sender = $this->getMockSender();
$result = array(
'key' => 'page_mail',
'params' => array(
'contact_message' => $message,
'sender' => $sender,
'contact_form' => $message->getContactForm(),
),
);
$results[] = $result + $default_result;
$result['key'] = 'page_autoreply';
$result['to'] = 'anonymous@drupal.org';
$result['from'] = NULL;
$results[] = $result + $default_result;
$data[] = array($message, $sender, $results);
$results = array();
$message = $this->getAnonymousMockMessage($recipients, '', TRUE);
$sender = $this->getMockSender();
$result = array(
'key' => 'page_mail',
'params' => array(
'contact_message' => $message,
'sender' => $sender,
'contact_form' => $message->getContactForm(),
),
);
$results[] = $result + $default_result;
$result['key'] = 'page_copy';
$result['to'] = 'anonymous@drupal.org';
$results[] = $result + $default_result;
$data[] = array($message, $sender, $results);
$results = array();
$message = $this->getAnonymousMockMessage($recipients, 'reply', TRUE);
$sender = $this->getMockSender();
$result = array(
'key' => 'page_mail',
'params' => array(
'contact_message' => $message,
'sender' => $sender,
'contact_form' => $message->getContactForm(),
),
);
$results[] = $result + $default_result;
$result['key'] = 'page_copy';
$result['to'] = 'anonymous@drupal.org';
$results[] = $result + $default_result;
$result['key'] = 'page_autoreply';
$result['from'] = NULL;
$results[] = $result + $default_result;
$data[] = array($message, $sender, $results);
//For authenticated user.
$results = array();
$message = $this->getAuthenticatedMockMessage();
$sender = $this->getMockSender(FALSE, 'user@drupal.org');
$result = array(
'module' => 'contact',
'key' => 'user_mail',
'to' => 'user2@drupal.org',
'langcode' => 'en',
'params' => array(
'contact_message' => $message,
'sender' => $sender,
'recipient' => $message->getPersonalRecipient(),
),
'from' => 'user@drupal.org',
);
$results[] = $result;
$data[] = array($message, $sender, $results);
$results = array();
$message = $this->getAuthenticatedMockMessage(TRUE);
$sender = $this->getMockSender(FALSE, 'user@drupal.org');
$result = array(
'module' => 'contact',
'key' => 'user_mail',
'to' => 'user2@drupal.org',
'langcode' => 'en',
'params' => array(
'contact_message' => $message,
'sender' => $sender,
'recipient' => $message->getPersonalRecipient(),
),
'from' => 'user@drupal.org',
);
$results[] = $result;
$result['key'] = 'user_copy';
$result['to'] = $result['from'];
$results[] = $result;
$data[] = array($message, $sender, $results);
return $data;
}
/**
* Builds a mock sender on given scenario.
*
* @param bool $anonymous
* TRUE if the sender is anonymous.
* @param string $mail_address
* The mail address of the user.
*
* @return \Drupal\Core\Session\AccountInterface|\PHPUnit_Framework_MockObject_MockObject
* Mock sender for testing.
*/
protected function getMockSender($anonymous = TRUE, $mail_address = 'anonymous@drupal.org') {
$sender = $this->getMock('\Drupal\Core\Session\AccountInterface');
$sender->expects($this->once())
->method('isAnonymous')
->willReturn($anonymous);
$sender->expects($this->any())
->method('getEmail')
->willReturn($mail_address);
$sender->expects($this->any())
->method('getDisplayName')
->willReturn('user');
// User ID 1 has special implications, use 3 instead.
$sender->expects($this->any())
->method('id')
->willReturn($anonymous ? 0 : 3);
if ($anonymous) {
// Anonymous user values set in params include updated values for name and
// mail.
$sender->name = 'Anonymous (not verified)';
$sender->mail = 'anonymous@drupal.org';
}
return $sender;
}
/**
* Builds a mock message from anonymous user.
*
* @param array $recipients
* An array of recipient email addresses.
* @param bool $auto_reply
* TRUE if auto reply is enable.
* @param bool $copy_sender
* TRUE if a copy should be sent, FALSE if not.
*
* @return \Drupal\contact\MessageInterface|\PHPUnit_Framework_MockObject_MockObject
* Mock message for testing.
*/
protected function getAnonymousMockMessage($recipients, $auto_reply, $copy_sender = FALSE) {
$message = $this->getMock('\Drupal\contact\MessageInterface');
$message->expects($this->any())
->method('getSenderName')
->willReturn('Anonymous');
$message->expects($this->once())
->method('getSenderMail')
->willReturn('anonymous@drupal.org');
$message->expects($this->any())
->method('isPersonal')
->willReturn(FALSE);
$message->expects($this->once())
->method('copySender')
->willReturn($copy_sender);
$message->expects($this->any())
->method('getContactForm')
->willReturn($this->getMockContactForm($recipients, $auto_reply));
return $message;
}
/**
* Builds a mock message from authenticated user.
*
* @param bool $copy_sender
* TRUE if a copy should be sent, FALSE if not.
*
* @return \Drupal\contact\MessageInterface|\PHPUnit_Framework_MockObject_MockObject
* Mock message for testing.
*/
protected function getAuthenticatedMockMessage($copy_sender = FALSE) {
$message = $this->getMock('\Drupal\contact\MessageInterface');
$message->expects($this->any())
->method('isPersonal')
->willReturn(TRUE);
$message->expects($this->once())
->method('copySender')
->willReturn($copy_sender);
$recipient = $this->getMock('\Drupal\user\UserInterface');
$recipient->expects($this->once())
->method('getEmail')
->willReturn('user2@drupal.org');
$recipient->expects($this->once())
->method('getDisplayName')
->willReturn('user2');
$recipient->expects($this->once())
->method('getPreferredLangcode')
->willReturn('en');
$message->expects($this->any())
->method('getPersonalRecipient')
->willReturn($recipient);
$message->expects($this->any())
->method('getContactForm')
->willReturn($this->getMockContactForm('user2@drupal.org', FALSE));
return $message;
}
/**
* Builds a mock message on given scenario.
*
* @param array $recipients
* An array of recipient email addresses.
* @param string $auto_reply
* An auto-reply message to send to the message author.
*
* @return \Drupal\contact\ContactFormInterface|\PHPUnit_Framework_MockObject_MockObject
* Mock message for testing.
*/
protected function getMockContactForm($recipients, $auto_reply) {
$contact_form = $this->getMock('\Drupal\contact\ContactFormInterface');
$contact_form->expects($this->once())
->method('getRecipients')
->willReturn($recipients);
$contact_form->expects($this->once())
->method('getReply')
->willReturn($auto_reply);
return $contact_form;
}
}