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?
| Comment | File | Size | Author |
|---|---|---|---|
| #2 | jamesan_387502-2.patch | 992 bytes | JamesAn |
Comments
Comment #1
karschsp commentedThis might be a good one for a novice.
Comment #2
JamesAn commentedSounds straight-forward enough. Voila.
Comment #3
catchI'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?
Comment #4
JamesAn commentedHmm..
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?
Comment #5
JamesAn commentedReview time?
Comment #6
damien tournoud commentedThe 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.
Comment #7
cwgordon7 commentedI 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.
Comment #8
nasso commented#2: jamesan_387502-2.patch queued for re-testing.
Comment #10
Niklas Fiekas commentedSide note: Some form tests do it like that:
Comment #11
Niklas Fiekas commentedActually I didn't want to won't fix this. Setting to active for discussion on what to do.
Comment #12
Niklas Fiekas commentedComment #13
mile23This issue is somewhat vague. D8 has http://api.drupal.org/api/drupal/modules%21taxonomy%21taxonomy.test/func...
Is there a problem with it?
Comment #14
illeace commentedFirst, 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:
If this is the case, maybe that could be clarified? If not, perhaps this issue should just be closed.
Comment #15
Patrick Storey commentedI 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
Comment #28
quietone commentedMore 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.