Project

General

Profile

Debugging CiviCRM cheatsheet » History » Version 18

Jon Goldberg, 06/02/2022 05:38 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
To debug, you must turn on the debugger in VS Code, then enable debugging in the address bar for the requests in question.
39
40 18 Jon Goldberg
#### civicrm-buildkit (mod_php)
41
The instructions above assume php-fpm.  To also debug mod_php (e.g. civicrm-buildkit), do the following:
42
* Use the same configuration file as under "Web Browser", but at `/etc/php/7.4/apache2/conf.d/xdebug.ini`.
43
* Edit `/etc/php/7.4/apache2/conf.d/xdebug.ini` and change the client port from `9000` to `9001`.
44
45 14 Jon Goldberg
#### Command Line (phpunit)
46
47 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`.
48 15 Jon Goldberg
 * Also change `xdebug.mode=debug,develop` to avoid some unnecessary warning noise.
49 14 Jon Goldberg
50 5 Jon Goldberg
* You need to start a debugging session in VS Code with "Listen for XDebug".
51 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).
52 2 Jon Goldberg
53
Once you've got all that:
54 1 Jon Goldberg
```shell
55 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
56 2 Jon Goldberg
```
57 6 Jon Goldberg
58 8 Jon Goldberg
### Command-line runs of standalone scripts
59 6 Jon Goldberg
```shell
60 13 Jon Goldberg
env XDEBUG_SESSION=VSCODE php myscript.php
61 6 Jon Goldberg
```
62 16 Jon Goldberg
63
### Debugging REST API calls in Ansible/curl
64 17 Jon Goldberg
Add an additional POST argument `XDEBUG_SESSION=VSCODE`.  In curl, just add `-d 'XDEBUG_SESSION=VSCODE'` anywhere in your command.
65
66
For Ansible, this might look like:
67 16 Jon Goldberg
68
```yaml
69
  - name: Shut up about Civi extensions (warnings only, 7 days)
70
    uri:
71
      url: "{{ primary_url }}/{{ endpoint }}"
72
      method: POST
73
      body:
74
        XDEBUG_SESSION: VSCODE
75
        entity: StatusPreference
76
        action: create
77
        json: "{{ {'name': 'checkExtensionsUpdates', 'ignore_severity': 3, 'hush_until': seven_days_hence } | to_json }}"
78
        api_key: "{{ crm_api_key }}"
79
        key: "{{ crm_site_key }}"
80
      body_format: form-urlencoded
81
      return_content: yes
82
```