We cannot write some tests for contrib because the following form AJAX does not work with drupalPostAjaxForm():
'#ajax' => array(
'callback' => '::updateTypeSettings',
// <-- Selector not provided here.
'effect' => 'fade',
),
...
public function updateTypeSettings(array &$form, FormStateInterface $form_state) {
$response = new AjaxResponse();
// Update options for entity type bundles.
$response->addCommand(new ReplaceCommand(
'#embed-type-settings-wrapper', // <-- Selector ID provided here
$form['type_settings']
));
return $response;
}
However, this does work:
'#ajax' => array(
'callback' => '::updateTypeSettings',
'wrapper' => 'embed-type-settings-wrapper', // <-- Selector ID provided here.
'effect' => 'fade',
),
...
public function updateTypeSettings(array &$form, FormStateInterface $form_state) {
$response = new AjaxResponse();
// Update options for entity type bundles.
$response->addCommand(new ReplaceCommand(
NULL, // <-- Selector not provided here.
$form['type_settings']
));
return $response;
}
And the code in WebTestBase::drupalProcessAjaxResponse that shows that it don't support arbitrary selectors in the AjaxResponse:
switch ($command['command']) {
case 'insert':
$wrapperNode = NULL;
// When a command doesn't specify a selector, use the
// #ajax['wrapper'] which is always an HTML ID.
if (!isset($command['selector'])) {
$wrapperNode = $xpath->query('//*[@id="' . $ajax_settings['wrapper'] . '"]')->item(0);
}
// @todo Ajax commands can target any jQuery selector, but these are
// hard to fully emulate with XPath. For now, just handle 'head'
// and 'body', since these are used by
// \Drupal\Core\Ajax\AjaxResponse::ajaxRender().
elseif (in_array($command['selector'], array('head', 'body'))) {
$wrapperNode = $xpath->query('//' . $command['selector'])->item(0);
}
if ($wrapperNode) {
...
Comments
Comment #2
dave reidThis adds support for selecting an ID in the command response.
Comment #3
dave reidComment #4
dave reidComment #6
lokapujyaI worked on adding a test. This is a copy of the insert test, but it can be modified to reproduce the issue. Will post this for now.
Comment #7
lokapujyaI'm not sure this is really the right place for the test. Now, I'm stuck because I don't know what the rest of the form looks like. I might have to install entity_embed to figure it out.
But I started modifying the test to first reproduce the part under (see interdiff):
Comment #8
killua99 commentedNeed to follow the code standard.
Like:
...
}
else {
...
The rest look good for me.
Comment #9
lokapujyaWell the test is not actually testing the problem yet. That part in #8 isn't supposed to be part of the patch, it just snuck in there from another issue i was working on. Do you have any ideas about the test? Is it the right place to put the test for the fix in #2?
Comment #18
quietone commentedTriaging issues in simpletest.module as part of the Bug Smash Initiative to determine if they should be in the Simpletest Project or core.
This looks like it a Phpunit issue, changing component.
Comment #19
berdirI don't think so. This is about the fake-ajax test stuff that's unique to Simpletest's ajax processing stuff, there's nothing like it in phpunit, we let the browser do that for us.