Drupal 8.0.0 beta 12. More info: https://www.drupal.org/node/2514176

This commit is contained in:
Pantheon Automation 2015-08-17 17:00:26 -07:00 committed by Greg Anderson
commit 9921556621
13277 changed files with 1459781 additions and 0 deletions

View file

@ -0,0 +1,33 @@
<?php
/**
* @file
* Database additions for Drupal\contact\Tests\ContactUpgradePathTest.
*
* This dump only contains data for the contact module. The
* drupal-7.filled.bare.php file is imported before this dump, so the two form
* the database structure expected in tests altogether.
*/
// Update the default category to that it is not selected.
db_update('contact')
->fields(array('selected' => '0'))
->condition('cid', '1')
->execute();
// Add a custom contact category.
db_insert('contact')->fields(array(
'category',
'recipients',
'reply',
'weight',
'selected'
))
->values(array(
'category' => 'Upgrade test',
'recipients'=> 'test1@example.com,test2@example.com',
'reply' => 'Test reply',
'weight' => 1,
'selected' => 1,
))
->execute();

View file

@ -0,0 +1,9 @@
# Schema for configuration files of the Contact Storage Test module.
contact.form.*.third_party.contact_storage_test:
type: mapping
label: 'Per-contact form storage settings'
mapping:
send_a_pony:
type: boolean
label: 'Send a Pony'

View file

@ -0,0 +1,9 @@
name: 'Contact test storage'
type: module
description: 'Tests that contact messages can be stored.'
package: Testing
version: VERSION
core: 8.x
dependencies:
- contact
- user

View file

@ -0,0 +1,23 @@
<?php
/**
* @file
* Contains install and update hooks.
*/
/**
* Implements hook_install().
*/
function contact_storage_test_install() {
$entity_manager = \Drupal::entityManager();
$entity_type = $entity_manager->getDefinition('contact_message');
// Recreate the original entity type definition, in order to notify the
// manager of what changed. The change of storage backend will trigger
// schema installation.
// @see contact_storage_test_entity_type_alter()
$original = clone $entity_type;
$original->setStorageClass('Drupal\Core\Entity\ContentEntityNullStorage');
$entity_manager->onEntityTypeUpdate($entity_type, $original);
}

View file

@ -0,0 +1,65 @@
<?php
/**
* @file
* Contains custom contact message functionality for ContactStorageTest.
*/
use Drupal\contact\ContactFormInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Form\FormStateInterface;
/**
* Implements hook_entity_base_field_info().
*/
function contact_storage_test_entity_base_field_info(\Drupal\Core\Entity\EntityTypeInterface $entity_type) {
if ($entity_type->id() == 'contact_message') {
$fields = array();
$fields['id'] = BaseFieldDefinition::create('integer')
->setLabel(t('Message ID'))
->setDescription(t('The message ID.'))
->setReadOnly(TRUE)
->setSetting('unsigned', TRUE);
return $fields;
}
}
/**
* Implements hook_entity_type_alter().
*/
function contact_storage_test_entity_type_alter(array &$entity_types) {
/** @var $entity_types \Drupal\Core\Entity\EntityTypeInterface[] */
// Set the controller class for nodes to an alternate implementation of the
// Drupal\Core\Entity\EntityStorageInterface interface.
$entity_types['contact_message']->setStorageClass('\Drupal\Core\Entity\Sql\SqlContentEntityStorage');
$keys = $entity_types['contact_message']->getKeys();
$keys['id'] = 'id';
$entity_types['contact_message']->set('entity_keys', $keys);
$entity_types['contact_message']->set('base_table', 'contact_message');
}
/**
* Implements hook_form_FORM_ID_alter() for contact_form_form().
*/
function contact_storage_test_form_contact_form_form_alter(&$form, FormStateInterface $form_state) {
/** @var \Drupal\contact\ContactFormInterface $contact_form */
$contact_form = $form_state->getFormObject()->getEntity();
$form['send_a_pony'] = array(
'#type' => 'checkbox',
'#title' => t('Send submitters a voucher for a free pony.'),
'#description' => t('Enable to send an additional email with a free pony voucher to anyone who submits the form.'),
'#default_value' => $contact_form->getThirdPartySetting('contact_storage_test', 'send_a_pony', FALSE),
);
$form['#entity_builders'][] = 'contact_storage_test_contact_form_form_builder';
}
/**
* Entity builder for the contact form edit form with third party options.
*
* @see contact_storage_test_form_contact_form_edit_form_alter()
*/
function contact_storage_test_contact_form_form_builder($entity_type, ContactFormInterface $contact_form, &$form, FormStateInterface $form_state) {
$contact_form->setThirdPartySetting('contact_storage_test', 'send_a_pony', $form_state->getValue('send_a_pony'));
}

View file

@ -0,0 +1,7 @@
id: feedback
label: 'Website feedback'
recipients: { }
reply: ''
weight: 0
status: true
langcode: en

View file

@ -0,0 +1,8 @@
name: 'Contact test module'
type: module
description: 'Contains test contact form.'
package: Testing
version: VERSION
core: 8.x
dependencies:
- contact

View file

@ -0,0 +1,10 @@
name: 'Contact test views'
type: module
description: 'Provides default views for views contact tests.'
package: Testing
version: VERSION
core: 8.x
dependencies:
- contact
- views
- user

View file

@ -0,0 +1,149 @@
langcode: en
status: true
dependencies:
module:
- contact
- user
id: test_contact_link
label: test_contact_link
module: views
description: ''
tag: ''
base_table: users_field_data
base_field: uid
core: 8.x
display:
default:
display_plugin: default
id: default
display_title: Master
position: 1
display_options:
access:
type: none
cache:
type: tag
options: { }
query:
type: views_query
options:
disable_sql_rewrite: false
distinct: false
replica: false
query_comment: ''
query_tags: { }
exposed_form:
type: basic
options:
submit_button: Apply
reset_button: false
reset_button_label: Reset
exposed_sorts_label: 'Sort by'
expose_sort_order: true
sort_asc_label: Asc
sort_desc_label: Desc
pager:
type: full
options:
items_per_page: 10
offset: 0
id: 0
total_pages: null
expose:
items_per_page: false
items_per_page_label: 'Items per page'
items_per_page_options: '5, 10, 20, 40, 60'
items_per_page_options_all: false
items_per_page_options_all_label: '- All -'
offset: false
offset_label: Offset
tags:
previous: ' previous'
next: 'next '
first: '« first'
last: 'last »'
quantity: 9
style:
type: default
row:
type: fields
fields:
name:
id: name
table: users_field_data
field: name
label: ''
alter:
alter_text: false
make_link: false
absolute: false
trim: false
word_boundary: false
ellipsis: false
strip_tags: false
html: false
hide_empty: false
empty_zero: false
relationship: none
group_type: group
admin_label: ''
exclude: false
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: true
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: true
empty: ''
hide_alter_empty: true
plugin_id: field
type: user_name
entity_type: user
entity_field: name
contact:
id: contact
table: users
field: contact
plugin_id: contact_link
exclude: false
entity_type: user
filters:
status:
value: true
table: users_field_data
field: status
id: status
expose:
operator: '0'
group: 1
plugin_id: boolean
entity_type: user
entity_field: status
sorts: { }
title: test_contact_link
header: { }
footer: { }
empty: { }
relationships: { }
arguments: { }
display_extenders: { }
cache_metadata:
contexts:
- 'languages:language_content'
- 'languages:language_interface'
cacheable: false
page_1:
display_plugin: page
id: page_1
display_title: Page
position: 1
display_options:
display_extenders: { }
path: test-contact-link
cache_metadata:
contexts:
- 'languages:language_content'
- 'languages:language_interface'
cacheable: false

View file

@ -0,0 +1,414 @@
<?php
/**
* @file
* Contains \Drupal\Tests\contact\Unit\MailHandlerTest.
*/
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('getUsername')
->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('getUsername')
->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;
}
}