diff --git a/core/modules/responsive_preview/lib/Drupal/responsive_preview/Form/DeviceDelete.php b/core/modules/responsive_preview/lib/Drupal/responsive_preview/Form/DeviceDelete.php index 8f98be5..41cce61 100644 --- a/core/modules/responsive_preview/lib/Drupal/responsive_preview/Form/DeviceDelete.php +++ b/core/modules/responsive_preview/lib/Drupal/responsive_preview/Form/DeviceDelete.php @@ -7,66 +7,42 @@ namespace Drupal\responsive_preview\Form; -use Drupal\Core\Form\ConfirmFormBase; +use Drupal\Core\Entity\EntityConfirmFormBase; use Drupal\responsive_preview\DeviceInterface; /** * Provides a deletion confirmation form for a device entity. */ -class DeviceDelete extends ConfirmFormBase { - - /** - * The device being deleted. - * - * @var \Drupal\responsive_preview\DeviceInterface - */ - protected $device; +class DeviceDelete extends EntityConfirmFormBase { /** * {@inheritdoc} */ - public function getFormID() { - return 'responsive_preview_admin_device_delete_confirm'; + public function getQuestion() { + return t('Are you sure you want to delete the device %name?', array('%name' => $this->entity->label())); } /** * {@inheritdoc} */ - protected function getQuestion() { - return t('Are you sure you want to delete the device %name?', array('%name' => $this->device->label())); - } - - /** - * {@inheritdoc} - */ - protected function getCancelPath() { + public function getCancelPath() { return 'admin/config/content/responsive-preview'; } /** * {@inheritdoc} */ - protected function getConfirmText() { + public function getConfirmText() { return t('Delete'); } /** * {@inheritdoc} - * @param \Drupal\responsive_preview\DeviceInterface $responsive_preview_device - * The device being deleted. - */ - public function buildForm(array $form, array &$form_state, DeviceInterface $responsive_preview_device = NULL) { - $this->device = $device; - return parent::buildForm($form, $form_state); - } - - /** - * {@inheritdoc} */ - public function submitForm(array &$form, array &$form_state) { - $this->device->delete(); - watchdog('responsive_preview', 'Device %name has been deleted.', array('%name' => $this->device->label())); - drupal_set_message(t('Device %name has been deleted.', array('%name' => $this->device->label()))); + public function submit(array $form, array &$form_state) { + $this->entity->delete(); + watchdog('responsive_preview', 'Device %name has been deleted.', array('%name' => $this->entity->label())); + drupal_set_message(t('Device %name has been deleted.', array('%name' => $this->entity->label()))); $form_state['redirect'] = 'admin/config/content/responsive-preview'; } diff --git a/core/modules/responsive_preview/lib/Drupal/responsive_preview/Plugin/Core/Entity/Device.php b/core/modules/responsive_preview/lib/Drupal/responsive_preview/Plugin/Core/Entity/Device.php index 8acb4ea..7e20a51 100644 --- a/core/modules/responsive_preview/lib/Drupal/responsive_preview/Plugin/Core/Entity/Device.php +++ b/core/modules/responsive_preview/lib/Drupal/responsive_preview/Plugin/Core/Entity/Device.php @@ -19,12 +19,13 @@ * label = @Translation("Responsive preview device"), * module = "responsive_preview", * controllers = { - "access" = "Drupal\responsive_preview\DeviceAccessController", + * "access" = "Drupal\responsive_preview\DeviceAccessController", * "storage" = "Drupal\Core\Config\Entity\ConfigStorageController", * "list" = "Drupal\responsive_preview\DeviceListController", * "form" = { * "edit" = "Drupal\responsive_preview\DeviceFormController", - * "add" = "Drupal\responsive_preview\DeviceFormController" + * "add" = "Drupal\responsive_preview\DeviceFormController", + * "delete" = "Drupal\responsive_preview\Form\DeviceDelete" * } * }, * config_prefix = "responsive_preview.device", diff --git a/core/modules/responsive_preview/lib/Drupal/responsive_preview/Tests/DeviceCRUDTest.php b/core/modules/responsive_preview/lib/Drupal/responsive_preview/Tests/DeviceCRUDTest.php new file mode 100644 index 0000000..d789b2d --- /dev/null +++ b/core/modules/responsive_preview/lib/Drupal/responsive_preview/Tests/DeviceCRUDTest.php @@ -0,0 +1,90 @@ + 'Responsive preview', + 'description' => 'Tests device management functionality.', + 'group' => 'Responsive preview', + ); + } + + /** + * Tests configuring devices. + */ + function testDeviceConfiguration() { + // Create and login administrative user. + $admin_user = $this->drupalCreateUser(array( + 'administer site configuration', + 'access toolbar', + )); + $this->drupalLogin($admin_user); + + $this->drupalGet('admin/config/content/responsive-preview'); + + // Some default devices exist. + $this->assertLinkByHref('admin/config/content/responsive-preview/manage/large/delete'); + $this->assertLinkByHref('admin/config/content/responsive-preview/manage/ipad/delete'); + + // Generic devices are shown by default. + $this->drupalGet(''); + $this->checkDevices(array('medium', 'small')); + + // Delete one of the predefined generic items. + $this->drupalPost('admin/config/content/responsive-preview/manage/medium/delete', array(), t('Delete')); + $this->assertRaw(t('Device %name has been deleted.', array('%name' => 'Tablet'))); + + // Make iPad appear in the list. + $this->drupalPost('admin/config/content/responsive-preview/manage/ipad', array('status' => 1), t('Save')); + $this->assertRaw(t('Device %name has been updated.', array('%name' => 'iPad'))); + + // Add a new device as well. + $edit = array( + 'label' => 'Smartwatch', + 'id' => 'smartwatch', + 'dimensions[width]' => '200', + 'dimensions[height]' => '350', + 'dimensions[dppx]' => '3', + 'status' => 1, + ); + $this->drupalPost('admin/config/content/responsive-preview/add', $edit, t('Save')); + $this->assertRaw(t('Device %name has been added.', array('%name' => 'Smartwatch'))); + + // Check updated device list applied. + $this->drupalGet(''); + $this->checkDevices(array('smartwatch', 'ipad', 'small')); + } + + /** + * Tests exposed devices in the responsive preview list. + */ + private function checkDevices(array $devices) { + foreach ($devices as $name) { + $device_button = $this->xpath('//button[@data-responsive-preview-name=:name]', array( + ':name' => $name + )); + $this->assertTrue(!empty($device_button), format_string('%name device shown by default', array('%name' => $name))); + } + } + +} diff --git a/core/modules/responsive_preview/responsive_preview.routing.yml b/core/modules/responsive_preview/responsive_preview.routing.yml index f8301c5..dcf7e63 100644 --- a/core/modules/responsive_preview/responsive_preview.routing.yml +++ b/core/modules/responsive_preview/responsive_preview.routing.yml @@ -23,6 +23,6 @@ responsive_preview_device_edit: responsive_preview_device_delete: pattern: '/admin/config/content/responsive-preview/manage/{responsive_preview_device}/delete' defaults: - _form: '\Drupal\responsive_preview\Form\DeviceDelete' + _entity_form: responsive_preview_device.delete requirements: _permission: 'administer site configuration'