Project

General

Profile

Debugging CiviCRM cheatsheet » History » Revision 25

Revision 24 (Brienne Kordis, 02/15/2023 07:06 PM) → Revision 25/47 (Brienne Kordis, 02/15/2023 07:08 PM)

{{last_updated_at}} by {{last_updated_by}} 

 # Debugging CiviCRM cheatsheet 

 ### Command-line runs of PHPUnit: 

 From a buildkit civiroot, run a specific file's tests:: 

 ```shell 
 CIVICRM_UF=UnitTests phpunit5 tests/phpunit/CRM/Core/BAO/AddressTest.php 
 ``` 

 You can also limit to a single test by filtering on name: 

 ```shell 
 CIVICRM_UF=UnitTests phpunit5 tests/phpunit/CRM/Core/BAO/AddressTest.php --filter testShared 
 ``` 

 ### With XDebug 

 #### Web Browser 
 * Install the xdebug extension (e.g. `sudo apt install php7.4-xdebug`). 
 * Configure xdebug by copying the values below to `/etc/php/7.4/fpm/conf.d/xdebug.ini`: 

 ``` 
 xdebug.mode=debug,develop 
 xdebug.start_with_request=trigger 
 xdebug.client_port=9000 
 xdebug.client_host = "127.0.0.1" 
 xdebug.log=/var/log/xdebug.log 
 #xdebug.mode=profile 
 #xdebug.output_dir = "/home/jon/temp/xdebug" 
 ``` 

 * Install a plugin for your browser, like "XDebug Helper for Firefox". 
  * In the plugin's configuration, set the IDE key to `VSCODE`. 

 * If one does not already exist, create a launch.json file within the .vscode folder (make a new folder if it does not yet exist) at the root of your codebase. What goes into the launch.json file depends on whether you are debugging a a site created with the `civibuild` command or one that was not built with that command. Here are examples of a [civibuild launch.json](https://hq.megaphonetech.com/projects/commons/wiki/Launchjson_for_civibuild_sites) and a [non-civibuild launch.json](https://hq.megaphonetech.com/projects/commons/wiki/Launchjson_for_non-civibuild_sites). Note that the "max-depth" on the "Listen for XDebug" configuration can be changed to delve deeper into the values that are returned (i.e. a depth of 6 will return more levels of a nested array than a depth of 3). 

 To debug, you must turn on the debugger in VS Code, then enable debugging in the address bar for the requests in question. 

 #### civicrm-buildkit (mod_php) 
 The instructions above assume php-fpm.    To also debug mod_php (e.g. civicrm-buildkit), do the following: 
 * Use the same configuration file as under "Web Browser", but at `/etc/php/7.4/apache2/conf.d/xdebug.ini`. 
 * Edit `/etc/php/7.4/apache2/conf.d/xdebug.ini` and change the client port from `9000` to `9001`. 

 #### Command Line (phpunit) 

 * You need to have XDebug otherwise configured for CLI.    Use the same configuration file as under "Web Browser", but at `/etc/php/7.4/cli/conf.d/xdebug.ini`. 
  * Also change `xdebug.mode=debug,develop` to avoid some unnecessary warning noise. 

 * You need to start a debugging session in VS Code with "Listen for XDebug". 
 * Depending on your VS Code setup, you may need to listen on a different port (I can use the same port for FPM but not mod_php). 

 Once you've got all that: 
 ```shell 
 env CIVICRM_UF=UnitTests XDEBUG_SESSION=VSCODE phpunit7 /home/jon/local/civicrm-buildkit/build/dmaster/web/sites/all/modules/civicrm/tests/phpunit/CRM/Core/BAO/ActionScheduleTest.php --filter testMembershipJoinDateMinutesUnit 
 ``` 

 ### Command-line runs of standalone scripts 
 ```shell 
 env XDEBUG_SESSION=VSCODE php myscript.php 
 ``` 

 ### Debugging REST API calls in Ansible/curl 
 Add an additional POST argument `XDEBUG_SESSION=VSCODE`.    In curl, just add `-d 'XDEBUG_SESSION=VSCODE'` anywhere in your command. 

 For Ansible, this might look like: 

 ```yaml 
   - name: Shut up about Civi extensions (warnings only, 7 days) 
     uri: 
       url: "{{ primary_url }}/{{ endpoint }}" 
       method: POST 
       body: 
         XDEBUG_SESSION: VSCODE 
         entity: StatusPreference 
         action: create 
         json: "{{ {'name': 'checkExtensionsUpdates', 'ignore_severity': 3, 'hush_until': seven_days_hence } | to_json }}" 
         api_key: "{{ crm_api_key }}" 
         key: "{{ crm_site_key }}" 
       body_format: form-urlencoded 
       return_content: yes 
 ``` 

 ### Debugging in PHP `file_get_contents()` (e.g. `check_civicrm.php`) 
 Add `XDEBUG_SESSION=VSCODE` as a `GET` argument, it works even on a `POST` request. Pairs well with a remote debugging session. 
 e.g.: 
 ```php 
 $url = "$prot://$host_address/$path/System/check?XDEBUG_SESSION=VSCODE"; 
 ``` 

 

 ### Debugging JavaScript 

 While you can open up the Developer Tools on a browser    (by right clicking on the window and then clicking **Inspect**) and debug the code directly there, it might be preferable to set up a debugging environment within an IDE, i.e. Visual Studio Code.    To do so: 

 1. Open up your launch.json file (Need to set one up? There are [non-civibuild](https://hq.megaphonetech.com/projects/commons/wiki/Launchjson_for_non-civibuild_sites and [Civibuild](https://hq.megaphonetech.com/projects/commons/wiki/Launchjson_for_civibuild_sites) options) Check out __ and__) 
 1. Within the    `”configurations:”` array, add a comma after the last curly brace and then paste in: 

 ``` json 
     { 
       "type": "msedge", 
       "request": "attach", 
       "name": "Attach to browser", 
       "port": 9222 
     } 
 ``` 
    * If you’d prefer to debug on chrome, use `”type”: “chrome”` 
 1. Within your terminal, run the following command: 

 ```bash 
 /usr/bin/google-chrome --remote-debugging-port=9000 –user-data-dir=remote-debug-profile 
 ``` 
   * `/usr/bin/google-chrome` is the path of the Google Chrome Binary on Linux. For other operating systems, check out this [comment](https://forum.katalon.com/t/unknown-error-cannot-find-chrome-binary/9008/5), but note that the exact name might be different (i.e. google-chrome1 vs google-chrome). You can always `cd`    through the path it recommends to find the exact match for either Chrome or MSEdge. 

 1. Within VS Code, go to the Run and Debug tab, and from the drop down menu at the top, select “Attach to browser” and start the debugger.  
    * Note that the command given in the previous step is configured to open up a new browser, but these arguments (and more) can be customized. See the [documentation](https://code.visualstudio.com/docs/nodejs/browser-debugging#_launch-configuration-attributes) 

 1. The new browser window (or tab, depending on your settings) is now configured to be the debuggee-yes, that’s the technical term- so you can set breakpoints and debug within VS Code!