diff --git a/core/modules/views_ui/lib/Drupal/views_ui/Form/Ajax/FormWrapper.php b/core/modules/views_ui/lib/Drupal/views_ui/Form/Ajax/FormWrapper.php index a60436c..7b1ea9a 100644 --- a/core/modules/views_ui/lib/Drupal/views_ui/Form/Ajax/FormWrapper.php +++ b/core/modules/views_ui/lib/Drupal/views_ui/Form/Ajax/FormWrapper.php @@ -55,7 +55,7 @@ public function wrapAjaxForm($form_id, array &$form_state) { // These forms have the title built in, so set the title here: if (empty($form_state['ajax']) && !empty($form_state['title'])) { - $output['#attached']['library'][] = array('views_ui', 'views_ui.admin'); + $form['#attached']['library'][] = array('views_ui', 'views_ui.admin'); } $output = $this->drupalRender($form); diff --git a/core/modules/views_ui/lib/Drupal/views_ui/Form/Ajax/ViewsFormBase.php b/core/modules/views_ui/lib/Drupal/views_ui/Form/Ajax/ViewsFormBase.php index 10619fc..1f811a4 100644 --- a/core/modules/views_ui/lib/Drupal/views_ui/Form/Ajax/ViewsFormBase.php +++ b/core/modules/views_ui/lib/Drupal/views_ui/Form/Ajax/ViewsFormBase.php @@ -109,7 +109,6 @@ public function getForm(ViewStorageInterface $view, $display_id, $js) { // @todo Remove the need for this. \Drupal::moduleHandler()->loadInclude('views_ui', 'inc', 'admin'); - \Drupal::moduleHandler()->loadInclude('views', 'inc', 'includes/ajax'); // Reset the cache of IDs. Drupal rather aggressively prevents ID // duplication but this causes it to remember IDs that are no longer even diff --git a/core/modules/views_ui/tests/Drupal/views_ui/Tests/Form/Ajax/FormWrapperTest.php b/core/modules/views_ui/tests/Drupal/views_ui/Tests/Form/Ajax/FormWrapperTest.php index 683fdae..5123c27 100644 --- a/core/modules/views_ui/tests/Drupal/views_ui/Tests/Form/Ajax/FormWrapperTest.php +++ b/core/modules/views_ui/tests/Drupal/views_ui/Tests/Form/Ajax/FormWrapperTest.php @@ -5,8 +5,176 @@ * Contains \Drupal\views_ui\Tests\Form\Ajax\FormWrapperTest. */ -namespace Drupal\views_ui\Tests\Form\Ajax; +namespace Drupal\views_ui\Tests\Form\Ajax { -class FormWrapperTest { +use Drupal\Core\Form\FormInterface; +use Drupal\Tests\UnitTestCase; +use Drupal\views_ui\Form\Ajax\FormWrapper; -} +/** + * Tests the ajax form wrapper. + * + * @see \Drupal\views_ui\Form\Ajax\FormWrapper + */ +class FormWrapperTest extends UnitTestCase { + + /** + * The tested form wrapper. + * + * @var \Drupal\views_ui\Form\Ajax\FormWrapper|\Drupal\views_ui\Tests\Form\Ajax\TestFormWrapper + */ + protected $formWrapper; + + /** + * The mocked form builder. + * + * @var \Drupal\Core\Form\FormBuilderInterface|\PHPUnit_Framework_MockObject_MockObject + */ + protected $formBuilder; + + /** + * {@inheritdoc} + */ + public static function getInfo() { + return array( + 'name' => 'Ajax form wrapper', + 'description' => 'Tests the ajax form wrapper', + 'group' => 'Views UI', + ); + } + + /** + * {@inheritdoc} + */ + protected function setUp() { + $this->formBuilder = $this->getMock('Drupal\Core\Form\FormBuilderInterface'); + $this->formWrapper = new TestFormWrapper($this->formBuilder); + $this->formBuilder->expects($this->any()) + ->method('buildForm') + ->will($this->returnCallback(function (FormInterface $form_object, $form_state) { + $form = array(); + return $form_object->buildForm($form, $form_state); + })); + } + + /** + * Tests wrapAjaxForm on a non AJAX request without a title. + */ + public function testWrapAjaxFormOnNonAjaxRequest() { + $form_object = new TestForm(); + $form_state = array(); + + $output = $this->formWrapper->wrapAjaxForm($form_object, $form_state); + $this->assertEquals('Test form', $output); + } + + /** + * Tests wrapAjaxForm on a non AJAX request but with a title. + */ + public function testWrapAjaxFormOnNonAjaxRequestWithTitle() { + $form_object = new TestForm(); + $form_state = array( + 'title' => 'Test title', + ); + + $output = $this->formWrapper->wrapAjaxForm($form_object, $form_state); + $this->assertEquals('Test form views_ui:views_ui.admin', $output); + } + + /** + * Tests wrapAjaxForm on a AJAX request. + */ + public function testWrapAjaxOnAjaxRequest() { + $form_object = new TestForm(); + $form_state = array( + 'ajax' => TRUE, + 'title' => 'Test title', + ); + + $output = $this->formWrapper->wrapAjaxForm($form_object, $form_state); + $this->assertInstanceOf('Drupal\Core\Ajax\AjaxResponse', $output); + + $commands = static::readAttribute($output, 'commands'); + + $dialog_command = $commands[0]; + $this->assertEquals('openDialog', $dialog_command['command']); + $this->assertEquals('views-ui-dialog', $dialog_command['dialogOptions']['dialogClass']); + $this->assertEquals('Test title', $dialog_command['dialogOptions']['title']); + $this->assertTrue($dialog_command['dialogOptions']['modal']); + } + + /** + * Tests wrapAjaxForm with a specified section. + */ + public function testWrapAjaxWithSection() { + $form_object = new TestForm(); + $form_state = array( + 'ajax' => TRUE, + '#section' => 'example-section', + ); + + $output = $this->formWrapper->wrapAjaxForm($form_object, $form_state); + $this->assertInstanceOf('Drupal\Core\Ajax\AjaxResponse', $output); + + $commands = static::readAttribute($output, 'commands'); + $highlight_command = $commands[1]; + + $this->assertEquals('openDialog', $commands[0]['command']); + + $this->assertEquals('viewsHighlight', $highlight_command['command']); + $this->assertEquals('.example-section', $highlight_command['selector']); + } + +} + +class TestFormWrapper extends FormWrapper { + + protected function drupalRender(array $elements) { + + if (isset($elements['#markup'])) { + $library = isset($elements['#attached']) ? ' ' . implode(':', $elements['#attached']['library'][0]) : ''; + return $elements['#markup'] . $library; + } + elseif (isset($elements['#theme']) && $elements['#theme'] == 'status_messages') { + return ''; + } + } + + protected function drupalCleanCssIdentifier($identifier) { + return $identifier; + } + +} + +class TestForm implements FormInterface { + + public function getFormId() { + return 'test_form'; + } + + public function buildForm(array $form, array &$form_state) { + $form = array( + '#markup' => 'Test form', + ); + return $form; + } + + public function validateForm(array &$form, array &$form_state) { + + } + + public function submitForm(array &$form, array &$form_state) { + + } + +} + +} + +namespace { + if (!function_exists('drupal_add_library')) { + function drupal_add_library() { + return TRUE; + } + } +}