Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I'm facing the following issue while writing tests for EntityEmbedDialog. I was trying to ensure that the form successfully shows the fields for 'embed' step when it's submitted for 'select' step. From what it seems, the form still displays the 'select' step even after I submit it using drupalPostAjaxForm(). Also, it throws no errors if I submit an invalid entity id. Here's the code for the test:
/**
* @file
* Contains \Drupal\entity_embed\Tests\EntityEmbedDialogTest.
*/
namespace Drupal\entity_embed\Tests;
/**
* Tests the entity_embed dialog controller and route.
*
* @group entity_embed
*/
class EntityEmbedDialogTest extends EntityEmbedTestBase {
/**
* Base URL of the dialog route.
*
* @var string
*/
protected $dialog_url;
protected function setUp() {
parent::setUp();
// Define URL that will be used to access the entity embed dialog.
$this->dialog_url = 'entity-embed/dialog/entity-embed/';
}
/**
* Tests the entity embed dialog.
*/
public function testEntityEmbedDialog() {
// Ensure that the route is not accessible without specifying all the
// parameters.
$this->DrupalGet($this->dialog_url);
$this->assertResponse(404, 'Embed dialog is not accessible without specifying filter format and embed button.');
$this->DrupalGet($this->dialog_url . 'custom_fromat');
$this->assertResponse(404, 'Embed dialog is not accessible without specifying embed button.');
// Ensure that the route is not accessible with an invalid embed button.
$this->DrupalGet($this->dialog_url . 'custom_format/invalid-embed-button');
$this->assertResponse(404, 'Embed dialog is not accessible without specifying filter format and embed button.');
// Ensure that the route is accessible with a valid embed button.
// 'Node' embed button is provided by default by the module and hence the
// request must be successful.
$this->DrupalGet($this->dialog_url . 'custom_format/node');
$this->assertResponse(200, 'Embed dialog is accessible with correct filter format and embed button.');
// Ensure form structure of the 'select' step and submit form.
$this->assertFieldByName('attributes[data-entity-id]', '', 'Entity ID/UUID field is present.');
$edit = array(
'attributes[data-entity-id]' => $this->node->id(),
);
$this->drupalPostAjaxForm(NULL, $edit, array('op' => t('Next')));
// Ensure form structure of the 'embed' step and submit form.
$this->assertFieldByName('attributes[data-entity-embed-display]', 'Display plugin field is present.');
}
}
I'm not sure what I'm doing wrong. Will be great if you could take a look.
Comment | File | Size | Author |
---|---|---|---|
#18 | 2328659-18.patch | 18.97 KB | phenaproxima |
| |||
#14 | 2328659-14.patch | 18.98 KB | phenaproxima |
| |||
#9 | 2328659-9-test.patch | 3.96 KB | lokapujya |
Comments
Comment #1
slashrsm CreditAttribution: slashrsm commentedComment #2
slashrsm CreditAttribution: slashrsm commentedComment #3
Dave ReidOoh, I think I've finally figured out why this doesn't work!
This works with drupalPostAjaxForm():
This does not:
I think I need to file a bug in Drupal 8 in drupalProcessAjaxResponse()
Comment #4
Dave ReidAnd here's the code in drupalProcessAjaxResponse that shows that they don't support arbitrary selectors in the AjaxResponse:
Comment #5
Dave ReidIt looks like BrowserTestBase should probably be used for complex AJAX testing: https://www.drupal.org/node/2232861. But it appears that it's not actually ready until Drupal 8.1. And lacks JavaScript so cannot actually be used for anything AJAX related: https://www.drupal.org/node/2469713.
Comment #6
Dave ReidAn example of a contrib module test converted to BrowserTestBase: https://www.drupal.org/node/2469609
Comment #7
Dave ReidFYI you should just be able to override drupalProcessAjaxResponse() in your base test class that extends WebTestBase to include the patch.
Comment #8
Dave ReidCore patch filed in #2554449: drupalProcessAjaxResponse() should support returning an AJAX InsertCommand with an ID selector
Comment #9
lokapujyaIs this still worth fixing? I worked on adding a test. This is a copy of the replace test, but it can be modified to reproduce the issue. Will post this for now. This was meant for the core issue.
Comment #11
lokapujya.
Comment #12
slashrsm CreditAttribution: slashrsm at MD Systems GmbH commentedPlease submit core patches in the core issue.
What is the plan here? Convert to BrowserTestBase?
Comment #13
Wim LeersAt this point in the Drupal 8 cycle, where nearly all
WebTestBase
tests in core have been converted toBrowserTestBase
, that seems the only sensible path forward. If we convertentity_embed
's tests to useBrowserTestBase
, it will also be ready for Drupal 9.Rescoping.
Comment #14
phenaproximaThis is a pretty sloppy patch, but it converts all the tests to BrowserTestBase (and, for the parts of EntityEmbedDialogTest which use the defunct drupalPostAjaxForm(), to WebDriverTestBase). This might also bring things back to green in HEAD, which would be damn good progress.
The tests definitely still need refinement and refactoring, but this is a good start.
Comment #15
Wim LeersGiven that the test coverage is currently in less-than-ideal state, and is failing in HEAD, I'm ignoring any and all nits (since I'd have lots of them for the tests in HEAD too), and focusing on only the things that truly matter: the surface area covered by tests, and minimizing change.
Why can we delete all this? This means we reduce test coverage.
Aha, we moved it here, because it needed JS to run those tests, since
BrowserTestBase
doesn't emulate the AJAXy stuff without executing JS likeWebTestBase
used to do.This gets the job done; it gets tests to not only work using the non-deprecated test system, but also makes them pass again.
Comment #16
Wim LeersThis blocks #2560787: Add tests for entity access.
Comment #17
Wim LeersThis also blocks:
Comment #18
phenaproximaRerolled against #2993391: test failing due to use_description_as_link_text change.
Comment #20
Wim Leers