Commit d7db0174 authored by catch's avatar catch

Issue #1588422 by andypost, sun, disasm, dawehner, vasi1186, alexpott,...

Issue #1588422 by andypost, sun, disasm, dawehner, vasi1186, alexpott, tim.plunkett, larowlan: Convert contact categories to configuration system.
parent a8650951
default_category: feedback
flood:
limit: '5'
interval: '3600'
......
......@@ -16,22 +16,40 @@
* @see contact_menu()
*/
function contact_category_list() {
drupal_set_title(t('Contact form categories'));
return entity_list_controller('contact_category')->render();
}
/**
* Page callback: Form constructor for the category creation form.
* Page callback: Presents the category creation form.
*
* @return string
* A processed form for the contact category.
* @return array
* A form array as expected by drupal_render().
*
* @see contact_menu()
*/
function contact_category_add() {
drupal_set_title(t('Add contact form category'));
$category = entity_create('contact_category', array());
return entity_get_form($category);
}
/**
* Page callback: Presents the category edit form.
*
* @param Drupal\contact\Plugin\Core\Entity\Category $category
* The contact category to edit.
*
* @return array
* A form array as expected by drupal_render().
*
* @see contact_menu()
*/
function contact_category_edit(Category $category) {
drupal_set_title(t('Edit %label contact form category', array('%label' => $category->label())), PASS_THROUGH);
return entity_get_form($category);
}
/**
* Page callback: Form constructor for the contact category deletion form.
*
......
......@@ -72,17 +72,24 @@ function contact_menu() {
'weight' => 1,
'file' => 'contact.admin.inc',
);
$items['admin/structure/contact/manage/%contact_category/edit'] = array(
$items['admin/structure/contact/manage/%contact_category'] = array(
'title' => 'Edit contact category',
'page callback' => 'entity_get_form',
'page callback' => 'contact_category_edit',
'page arguments' => array(4),
'access arguments' => array('administer contact forms'),
'file' => 'contact.admin.inc',
);
$items['admin/structure/contact/manage/%contact_category/edit'] = array(
'title' => 'Edit',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items['admin/structure/contact/manage/%contact_category/delete'] = array(
'title' => 'Delete contact category',
'title' => 'Delete',
'page callback' => 'drupal_get_form',
'page arguments' => array('contact_category_delete_form', 4),
'access arguments' => array('administer contact forms'),
'type' => MENU_LOCAL_TASK,
'file' => 'contact.admin.inc',
);
$items['contact'] = array(
......@@ -217,7 +224,7 @@ function contact_category_load($id) {
/**
* Entity URI callback.
*
* @param Drupal\category\Category $category
* @param Drupal\contact\Plugin\Core\Entity\Category $category
* A contact category entity.
*
* @return array
......
......@@ -90,58 +90,45 @@ public function validate(array $form, array &$form_state) {
$form_state['values']['recipients'] = $recipients;
}
/**
* Overrides Drupal\Core\Entity\EntityFormController::submit().
*/
public function submit(array $form, array &$form_state) {
// @todo We should not be calling contact_category_delete_form() from
// within the form builder.
if ($form_state['triggering_element']['#value'] == t('Delete')) {
// Rebuild the form to confirm category deletion.
$form_state['redirect'] = 'admin/structure/contact/manage/' . $form_state['values']['id'] . '/delete';
return NULL;
}
return parent::submit($form, $form_state);
}
/**
* Overrides Drupal\Core\Entity\EntityFormController::save().
*/
public function save(array $form, array &$form_state) {
$category = $this->getEntity($form_state);
// Property enforceIsNew is not supported by config entity. So this is only
// way to make sure that entity is not saved.
$is_new = !$category->getOriginalID();
$category->save();
$id = $category->id();
$status = $category->save();
if ($is_new) {
drupal_set_message(t('Category %label has been added.', array('%label' => $category->label())));
watchdog('contact', 'Category %label has been added.', array('%label' => $category->label()), WATCHDOG_NOTICE, l(t('Edit'), 'admin/structure/contact/manage/' . $id . '/edit'));
if ($status == SAVED_UPDATED) {
drupal_set_message(t('Category %label has been updated.', array('%label' => $category->label())));
watchdog('contact', 'Category %label has been updated.', array('%label' => $category->label()), WATCHDOG_NOTICE, l(t('Edit'), $category->uri() . '/edit'));
}
else {
drupal_set_message(t('Category %label has been updated.', array('%label' => $category->label())));
watchdog('contact', 'Category %label has been updated.', array('%label' => $category->label()), WATCHDOG_NOTICE, l(t('Edit'), 'admin/structure/contact/manage/' . $id . '/edit'));
drupal_set_message(t('Category %label has been added.', array('%label' => $category->label())));
watchdog('contact', 'Category %label has been added.', array('%label' => $category->label()), WATCHDOG_NOTICE, l(t('Edit'), $category->uri() . '/edit'));
}
// Update the default category.
$contact_config = config('contact.settings');
if ($form_state['values']['selected']) {
$contact_config
->set('default_category', $id)
->set('default_category', $category->id())
->save();
}
// If it was the default category, empty out the setting.
elseif ($contact_config->get('default_category') == $id) {
elseif ($contact_config->get('default_category') == $category->id()) {
$contact_config
->clear('default_category')
->set('default_category', NULL)
->save();
}
// Remove the 'selected' value, which is not part of the Category.
unset($form_state['values']['selected']);
$form_state['redirect'] = 'admin/structure/contact';
}
/**
* Overrides Drupal\Core\Entity\EntityFormController::delete().
*/
public function delete(array $form, array &$form_state) {
$category = $this->getEntity($form_state);
$form_state['redirect'] = 'admin/structure/contact/manage/' . $category->id() . '/delete';
}
}
......@@ -47,7 +47,7 @@ function testSiteWideContact() {
$edit = array();
$edit['contact_default_status'] = TRUE;
$this->drupalPost('admin/config/people/accounts', $edit, t('Save configuration'));
$this->assertText(t('The configuration options have been saved.'), 'Setting successfully saved.');
$this->assertText(t('The configuration options have been saved.'));
// Delete old categories to ensure that new categories are used.
$this->deleteCategories();
......@@ -67,19 +67,19 @@ function testSiteWideContact() {
$invalid_recipients = array('invalid', 'invalid@', 'invalid@site.', '@site.', '@site.com');
foreach ($invalid_recipients as $invalid_recipient) {
$this->addCategory($this->randomName(16), $this->randomName(16), $invalid_recipient, '', FALSE);
$this->assertRaw(t('%recipient is an invalid e-mail address.', array('%recipient' => $invalid_recipient)), format_string('Caught invalid recipient (@invalid_recipient)', array('@invalid_recipient' => $invalid_recipient)));
$this->assertRaw(t('%recipient is an invalid e-mail address.', array('%recipient' => $invalid_recipient)));
}
// Test validation of empty category and recipients fields.
$this->addCategory('', '', '', '', TRUE);
$this->assertText(t('Label field is required.'), 'Caught empty category label field');
$this->assertText(t('Machine-readable name field is required.'), 'Caught empty category name field');
$this->assertText(t('Recipients field is required.'), 'Caught empty recipients field.');
$this->assertText(t('Label field is required.'));
$this->assertText(t('Machine-readable name field is required.'));
$this->assertText(t('Recipients field is required.'));
// Create first valid category.
$recipients = array('simpletest@example.com', 'simpletest2@example.com', 'simpletest3@example.com');
$this->addCategory($id = drupal_strtolower($this->randomName(16)), $label = $this->randomName(16), implode(',', array($recipients[0])), '', TRUE);
$this->assertRaw(t('Category %label has been added.', array('%label' => $label)), 'Category successfully added.');
$this->assertRaw(t('Category %label has been added.', array('%label' => $label)));
// Make sure the newly created category is included in the list of categories.
$this->assertNoUniqueText($label, 'New category included in categories list.');
......@@ -91,27 +91,27 @@ function testSiteWideContact() {
$this->assertEqual($config['recipients'], array($recipients[0], $recipients[1]));
$this->assertEqual($config['reply'], $reply);
$this->assertNotEqual($id, config('contact.settings')->get('default_category'));
$this->assertRaw(t('Category %label has been updated.', array('%label' => $label)), 'Category successfully updated.');
$this->assertRaw(t('Category %label has been updated.', array('%label' => $label)));
// Ensure that the contact form is shown without a category selection input.
user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, array('access site-wide contact form'));
$this->drupalLogout();
$this->drupalGet('contact');
$this->assertText(t('Your e-mail address'), 'Contact form is shown when there is one category.');
$this->assertNoText(t('Category'), 'When there is only one category, the category selection element is hidden.');
$this->assertText(t('Your e-mail address'));
$this->assertNoText(t('Category'));
$this->drupalLogin($admin_user);
// Add more categories.
$this->addCategory(drupal_strtolower($this->randomName(16)), $label = $this->randomName(16), implode(',', array($recipients[0], $recipients[1])), '', FALSE);
$this->assertRaw(t('Category %label has been added.', array('%label' => $label)), 'Category successfully added.');
$this->assertRaw(t('Category %label has been added.', array('%label' => $label)));
$this->addCategory($name = drupal_strtolower($this->randomName(16)), $label = $this->randomName(16), implode(',', array($recipients[0], $recipients[1], $recipients[2])), '', FALSE);
$this->assertRaw(t('Category %label has been added.', array('%label' => $label)), 'Category successfully added.');
$this->assertRaw(t('Category %label has been added.', array('%label' => $label)));
// Try adding a category that already exists.
$this->addCategory($name, $label, '', '', FALSE);
$this->assertNoRaw(t('Category %label has been saved.', array('%label' => $label)), 'Category not saved.');
$this->assertRaw(t('The machine-readable name is already in use. It must be unique.'), 'Duplicate category error found.');
$this->assertNoRaw(t('Category %label has been saved.', array('%label' => $label)));
$this->assertRaw(t('The machine-readable name is already in use. It must be unique.'));
// Clear flood table in preparation for flood test and allow other checks to complete.
db_delete('flood')->execute();
......@@ -122,52 +122,52 @@ function testSiteWideContact() {
// Check to see that anonymous user cannot see contact page without permission.
user_role_revoke_permissions(DRUPAL_ANONYMOUS_RID, array('access site-wide contact form'));
$this->drupalGet('contact');
$this->assertResponse(403, 'Access denied to anonymous user without permission.');
$this->assertResponse(403);
// Give anonymous user permission and see that page is viewable.
user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, array('access site-wide contact form'));
$this->drupalGet('contact');
$this->assertResponse(200, 'Access granted to anonymous user with permission.');
$this->assertResponse(200);
// Submit contact form with invalid values.
$categories = entity_load_multiple('contact_category');
$id = key($categories);
$this->submitContact('', $recipients[0], $this->randomName(16), $id, $this->randomName(64));
$this->assertText(t('Your name field is required.'), 'Name required.');
$this->assertText(t('Your name field is required.'));
$this->submitContact($this->randomName(16), '', $this->randomName(16), $id, $this->randomName(64));
$this->assertText(t('Your e-mail address field is required.'), 'E-mail required.');
$this->assertText(t('Your e-mail address field is required.'));
$this->submitContact($this->randomName(16), $invalid_recipients[0], $this->randomName(16), $id, $this->randomName(64));
$this->assertRaw(t('The e-mail address %mail is not valid.', array('%mail' => 'invalid')), 'Valid e-mail required.');
$this->assertRaw(t('The e-mail address %mail is not valid.', array('%mail' => 'invalid')));
$this->submitContact($this->randomName(16), $recipients[0], '', $id, $this->randomName(64));
$this->assertText(t('Subject field is required.'), 'Subject required.');
$this->assertText(t('Subject field is required.'));
$this->submitContact($this->randomName(16), $recipients[0], $this->randomName(16), $id, '');
$this->assertText(t('Message field is required.'), 'Message required.');
$this->assertText(t('Message field is required.'));
// Test contact form with no default category selected.
config('contact.settings')
->set('default_category', '')
->save();
$this->drupalGet('contact');
$this->assertRaw(t('- Select -'), 'Without selected categories the visitor is asked to chose a category.');
$this->assertRaw(t('- Select -'));
// Submit contact form with invalid category id (cid 0).
$this->submitContact($this->randomName(16), $recipients[0], $this->randomName(16), 0, '');
$this->assertText(t('Category field is required.'), 'Valid category required.');
$this->assertText(t('Category field is required.'));
// Submit contact form with correct values and check flood interval.
for ($i = 0; $i < $flood_limit; $i++) {
$this->submitContact($this->randomName(16), $recipients[0], $this->randomName(16), $id, $this->randomName(64));
$this->assertText(t('Your message has been sent.'), 'Message sent.');
$this->assertText(t('Your message has been sent.'));
}
// Submit contact form one over limit.
$this->drupalGet('contact');
$this->assertResponse(403, 'Access denied to anonymous user after reaching message treshold.');
$this->assertRaw(t('You cannot send more than %number messages in @interval. Try again later.', array('%number' => config('contact.settings')->get('flood.limit'), '@interval' => format_interval(600))), 'Message threshold reached.');
$this->assertResponse(403);
$this->assertRaw(t('You cannot send more than %number messages in @interval. Try again later.', array('%number' => config('contact.settings')->get('flood.limit'), '@interval' => format_interval(600))));
// Delete created categories.
$this->drupalLogin($admin_user);
......@@ -299,7 +299,7 @@ function deleteCategories() {
$categories = entity_load_multiple('contact_category');
foreach ($categories as $id => $category) {
$this->drupalPost("admin/structure/contact/manage/$id/delete", array(), t('Delete'));
$this->assertRaw(t('Category %label has been deleted.', array('%label' => $category->label())), 'Category deleted successfully.');
$this->assertRaw(t('Category %label has been deleted.', array('%label' => $category->label())));
$this->assertFalse(entity_load('contact_category', $id), format_string('Category %category not found', array('%category' => $category->label())));
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment