While writing some patches for taxonomy_autocomplete, I found myself unable to easily write SimpleTest cases to test it, since it changes the browser header and outputs JSON.

I tried to test by buffering the output of the function, but had problems because json_decode didn't always successfully convert the result to a non-null object, even when the JSON result appeared valid.

This would be easier to test if the code in the taxonomy_autocomplete function broken out into a helper function that would return an array. This way, the helper function could be tested without dealing with JSON, and the taxonomy_autocomplete function could just serve as the callback to serve JSON to the browser.

The code would look something like this:

<?php
function taxonomy_autocomplete($vid, $string='') {
  $matches = _taxonomy_autocomplete($vid, $string);
  drupal_json($matches);
}

function _taxonomy_autocomplete($vid, $string='') {
  // insert code from old taxonomy_autocomplete, but return the array of matches instead of outputting JSON.
}
?>

Does anyone have any comments or suggestions on this?

CommentFileSizeAuthor
#2 jamesan_387502-2.patch992 bytesJamesAn

Comments

karschsp’s picture

Issue tags: +Novice

This might be a good one for a novice.

JamesAn’s picture

Status: Active » Needs review
StatusFileSize
new992 bytes

Sounds straight-forward enough. Voila.

catch’s picture

Title: Create helper function for taxonomy_autocomplete so simpletests can be written easily » SimpleTest can't read json easily
Status: Needs review » Needs work

I'm not sure about changing this just to make testing easier, it makes the code harder to read. Could we not add a helper in simpletest for assertions on JSON output?

JamesAn’s picture

Hmm..

I can't think of how to create a helper function for SimpleTest without repeating that block of code that processes the vid and csv string of tags. Intercepting the matches immediately before the call to drupal_json() would be messy, I think.

Could we just add inline comments to either taxonomy_autocomplete or _taxonomy_autocomplete to clarify things?

JamesAn’s picture

Status: Needs work » Needs review

Review time?

damien tournoud’s picture

Status: Needs review » Closed (won't fix)

The correct way to test that is to make a call over HTTP to the child site. I don't see any benefit in not doing it this way.

cwgordon7’s picture

Status: Closed (won't fix) » Needs work

I agree with catch in #3, we should add a helper function in DrupalWebTestCase for parsing JSON content, I similarly found it hard to right this sort of test with the Synonym Collapsing contributed module. Perhaps we could add a helper function that would decode the JSON output back into a PHP array for simpler asserting? Or an ->assertJSON(array('match1', 'match2')) function that would check that the current page content is (a) JSON data and (b) matches the PHP array given. I don't see any reason for this to be a won't fix.

nasso’s picture

Status: Needs work » Needs review
Issue tags: -Novice

#2: jamesan_387502-2.patch queued for re-testing.

Status: Needs review » Needs work
Issue tags: +Novice

The last submitted patch, jamesan_387502-2.patch, failed testing.

Niklas Fiekas’s picture

Status: Needs work » Closed (won't fix)

Side note: Some form tests do it like that:

  $foo = json_decode($this->drupalPost(...));
  $bar = json_decode($this->drupalget(...));
Niklas Fiekas’s picture

Status: Closed (won't fix) » Active

Actually I didn't want to won't fix this. Setting to active for discussion on what to do.

Niklas Fiekas’s picture

Version: 7.x-dev » 8.x-dev
mile23’s picture

This issue is somewhat vague. D8 has http://api.drupal.org/api/drupal/modules%21taxonomy%21taxonomy.test/func...

Is there a problem with it?

illeace’s picture

First, here's the link to this test for D8:
https://api.drupal.org/api/drupal/core!modules!taxonomy!lib!Drupal!taxon...

Second, it seems like the original post is saying there is a bug/issue with json_decode:

... had problems because json_decode didn't always successfully convert the result to a non-null object

If this is the case, maybe that could be clarified? If not, perhaps this issue should just be closed.

Patrick Storey’s picture

Issue summary: View changes
Issue tags: -Novice

I am removing the Novice tag from this issue because there seems to be a lack of consensus on what the next steps are.

I’m using this documentation as a source: https://www.drupal.org/core-mentoring/novice-tasks#avoid

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

quietone’s picture

Status: Active » Closed (outdated)

More information was asked in this issue 10 years ago. That has not been supplied, there has been no further discussion and much has changed since this issue was opened. I am closing at outdated.