2

There are several methods in CiviCRM that just print their output directly to the buffer and then exit, because they're just sending an HTML snippet or other response for an AJAX call. For example, CRM_Event_Cart_Page_CheckoutAJAX::add_participant_to_cart().

What are best practices for (or existing examples of) unit tests that cover such methods? I could, I suppose, use ob functions to capture the output, but then I'd need an external library to parse and test the HTML. Are there better ways, and are there examples already in place?

2

Allan - I have tended to try to refactor the AJAX functions such that they call a more testable function & test that - this generally improves the code....

There are a few examples that test the calls directly - see classes like

CRM_Contact_Page_AjaxTest CRM_Custom_Page_AJAXTest CRM_Financial_Page_AjaxTest CRM_Group_Page_AjaxTest

2

I'm a bit out of date on the current status of the civi test framework, but if WebTests are still available I would do this as a WebTest. https://wiki.civicrm.org/confluence/display/CRM/Setting+yourself+up+to+work+with+Selenium+tests

Include a line that waits for some element you are expecting to appear, then test its value using the webtest api.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.