Debugging CiviCRM cheatsheet » History » Version 28
  Jon Goldberg, 02/16/2023 04:40 PM 
  
| 1 | 7 | Jon Goldberg | {{last_updated_at}} by {{last_updated_by}} | 
|---|---|---|---|
| 2 | |||
| 3 | 1 | Jon Goldberg | # Debugging CiviCRM cheatsheet | 
| 4 | |||
| 5 | ### Command-line runs of PHPUnit: | ||
| 6 | |||
| 7 | From a buildkit civiroot, run a specific file's tests:: | ||
| 8 | |||
| 9 | 2 | Jon Goldberg | ```shell | 
| 10 | CIVICRM_UF=UnitTests phpunit5 tests/phpunit/CRM/Core/BAO/AddressTest.php | ||
| 11 | ``` | ||
| 12 | 1 | Jon Goldberg | |
| 13 | You can also limit to a single test by filtering on name: | ||
| 14 | |||
| 15 | 2 | Jon Goldberg | ```shell | 
| 16 | CIVICRM_UF=UnitTests phpunit5 tests/phpunit/CRM/Core/BAO/AddressTest.php --filter testShared | ||
| 17 | ``` | ||
| 18 | 3 | Jon Goldberg | |
| 19 | ### With XDebug | ||
| 20 | 9 | Jon Goldberg | |
| 21 | 14 | Jon Goldberg | #### Web Browser | 
| 22 | * Install the xdebug extension (e.g. `sudo apt install php7.4-xdebug`). | ||
| 23 | * Configure xdebug by copying the values below to `/etc/php/7.4/fpm/conf.d/xdebug.ini`: | ||
| 24 | 2 | Jon Goldberg | |
| 25 | 1 | Jon Goldberg | ``` | 
| 26 | 12 | Jon Goldberg | xdebug.mode=debug,develop | 
| 27 | xdebug.start_with_request=trigger | ||
| 28 | 1 | Jon Goldberg | xdebug.client_port=9000 | 
| 29 | xdebug.client_host = "127.0.0.1" | ||
| 30 | 10 | Jon Goldberg | xdebug.log=/var/log/xdebug.log | 
| 31 | 12 | Jon Goldberg | #xdebug.mode=profile | 
| 32 | 1 | Jon Goldberg | #xdebug.output_dir = "/home/jon/temp/xdebug" | 
| 33 | ``` | ||
| 34 | 14 | Jon Goldberg | |
| 35 | * Install a plugin for your browser, like "XDebug Helper for Firefox". | ||
| 36 | * In the plugin's configuration, set the IDE key to `VSCODE`. | ||
| 37 | |||
| 38 | 22 | Brienne Kordis | * 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). | 
| 39 | 19 | Brienne Kordis | |
| 40 | 14 | Jon Goldberg | To debug, you must turn on the debugger in VS Code, then enable debugging in the address bar for the requests in question. | 
| 41 | |||
| 42 | 28 | Jon Goldberg | #### Web Browser - remote debugging | 
| 43 | To debug on a remote server, ensure you have local debugging working first. | ||
| 44 | |||
| 45 | #### Server-side setup | ||
| 46 | * `sudo apt install php{{php_version}}-xdebug` (I should add this to `group_vars/all.yml` so it's everywhere). | ||
| 47 | * Need to add this somewhere that makes sense in PHP config: | ||
| 48 | |||
| 49 | ``` | ||
| 50 | xdebug.mode=debug | ||
| 51 | xdebug.start_with_request=trigger | ||
| 52 | ``` | ||
| 53 | * Restart PHP. | ||
| 54 | |||
| 55 | #### Client side setup | ||
| 56 | * In your VS Code plugins screen, you need to select `Install in SSH` for the `PHP Debug` and `PHP Intelephense` plugins. | ||
| 57 | * Using the new **Remote Explorer** sidebar icon in VS Code, select the server you'd like to connect to. | ||
| 58 | * If you've already debugged on this site before, you should be able to expand the server and see a workspace file (see screenshot below). Click one of the connect icons. | ||
| 59 | * If you haven't debugged on this site before, follow *First Time* instructions below. Otherwise, debug as normal (make sure your Firefox "debug" icon is turned on). | ||
| 60 | |||
| 61 | ##### First Time | ||
| 62 | * Click **Open Folder** and navigate to the site's gitroot (usually the same as webroot, but for D8+ it's the folder above `web` so you can also debug in `vendor`). | ||
| 63 | * Select **File menu » Save Workspace As** and store a workspace file in your home directory. | ||
| 64 | * Go to the VS Code "debug" sidebar. Click **create a launch.json file** and then select **workspace**, then your web browser. | ||
| 65 | * (Optional) in the **Remote Explorer** sidebar, under the server you're connected to, right-click the entry that doesn't say "Workspace" in it and select **Remove from Recent List** to only keep workspace entries. | ||
| 66 | |||
| 67 | |||
| 68 | 18 | Jon Goldberg | #### civicrm-buildkit (mod_php) | 
| 69 | The instructions above assume php-fpm. To also debug mod_php (e.g. civicrm-buildkit), do the following: | ||
| 70 | * Use the same configuration file as under "Web Browser", but at `/etc/php/7.4/apache2/conf.d/xdebug.ini`. | ||
| 71 | * Edit `/etc/php/7.4/apache2/conf.d/xdebug.ini` and change the client port from `9000` to `9001`. | ||
| 72 | |||
| 73 | 14 | Jon Goldberg | #### Command Line (phpunit) | 
| 74 | |||
| 75 | 1 | Jon Goldberg | * 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`. | 
| 76 | 15 | Jon Goldberg | * Also change `xdebug.mode=debug,develop` to avoid some unnecessary warning noise. | 
| 77 | 14 | Jon Goldberg | |
| 78 | 5 | Jon Goldberg | * You need to start a debugging session in VS Code with "Listen for XDebug". | 
| 79 | 1 | Jon Goldberg | * 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). | 
| 80 | 2 | Jon Goldberg | |
| 81 | Once you've got all that: | ||
| 82 | 1 | Jon Goldberg | ```shell | 
| 83 | 15 | Jon Goldberg | 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 | 
| 84 | 2 | Jon Goldberg | ``` | 
| 85 | 6 | Jon Goldberg | |
| 86 | 8 | Jon Goldberg | ### Command-line runs of standalone scripts | 
| 87 | 6 | Jon Goldberg | ```shell | 
| 88 | 13 | Jon Goldberg | env XDEBUG_SESSION=VSCODE php myscript.php | 
| 89 | 6 | Jon Goldberg | ``` | 
| 90 | 16 | Jon Goldberg | |
| 91 | ### Debugging REST API calls in Ansible/curl | ||
| 92 | 17 | Jon Goldberg | Add an additional POST argument `XDEBUG_SESSION=VSCODE`. In curl, just add `-d 'XDEBUG_SESSION=VSCODE'` anywhere in your command. | 
| 93 | |||
| 94 | For Ansible, this might look like: | ||
| 95 | 16 | Jon Goldberg | |
| 96 | ```yaml | ||
| 97 | - name: Shut up about Civi extensions (warnings only, 7 days) | ||
| 98 | uri: | ||
| 99 |       url: "{{ primary_url }}/{{ endpoint }}" | ||
| 100 | method: POST | ||
| 101 | body: | ||
| 102 | XDEBUG_SESSION: VSCODE | ||
| 103 | entity: StatusPreference | ||
| 104 | action: create | ||
| 105 |         json: "{{ {'name': 'checkExtensionsUpdates', 'ignore_severity': 3, 'hush_until': seven_days_hence } | to_json }}" | ||
| 106 |         api_key: "{{ crm_api_key }}" | ||
| 107 |         key: "{{ crm_site_key }}" | ||
| 108 | body_format: form-urlencoded | ||
| 109 | return_content: yes | ||
| 110 | ``` | ||
| 111 | 23 | Jon Goldberg | |
| 112 | ### Debugging in PHP `file_get_contents()` (e.g. `check_civicrm.php`) | ||
| 113 | Add `XDEBUG_SESSION=VSCODE` as a `GET` argument, it works even on a `POST` request. Pairs well with a remote debugging session. | ||
| 114 | e.g.: | ||
| 115 | ```php | ||
| 116 | $url = "$prot://$host_address/$path/System/check?XDEBUG_SESSION=VSCODE"; | ||
| 117 | ``` | ||
| 118 | 24 | Brienne Kordis | |
| 119 | ### Debugging JavaScript | ||
| 120 | |||
| 121 | 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: | ||
| 122 | |||
| 123 | 27 | Brienne Kordis | 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) | 
| 124 | 24 | Brienne Kordis | 1. Within the `”configurations:”` array, add a comma after the last curly brace and then paste in: | 
| 125 | |||
| 126 | ``` json | ||
| 127 |     { | ||
| 128 | "type": "msedge", | ||
| 129 | "request": "attach", | ||
| 130 | "name": "Attach to browser", | ||
| 131 | "port": 9222 | ||
| 132 | } | ||
| 133 | ``` | ||
| 134 | * If you’d prefer to debug on chrome, use `”type”: “chrome”` | ||
| 135 | 1. Within your terminal, run the following command: | ||
| 136 | |||
| 137 | ```bash | ||
| 138 | 26 | Brienne Kordis | /usr/bin/google-chrome --remote-debugging-port=9222 –user-data-dir=remote-debug-profile | 
| 139 | 24 | Brienne Kordis | ``` | 
| 140 | * `/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. | ||
| 141 | |||
| 142 | 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. | ||
| 143 | * 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) | ||
| 144 | |||
| 145 | 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! |