| 1 |
<?php |
| 2 |
|
| 3 |
namespace Tests\Browser; |
| 4 |
|
| 5 |
use function Tests\{asAdmin, asAuthor, asEditor}; |
| 6 |
|
| 7 |
const DEFAULT_SCRIPT_SELECTOR = 'script[src="https://scripts.simpleanalyticscdn.com/latest.js"]'; |
| 8 |
const INACTIVE_ADMIN_SCRIPT_SELECTOR = 'script[src="http://localhost:8100/wp-content/plugins/simpleanalytics/resources/js/inactive.js"]'; |
| 9 |
const INACTIVE_ADMIN_COMMENT = '<!-- Simple Analytics: Not logging requests from admins -->'; |
| 10 |
const NOSCRIPT_SELECTOR = 'noscript img[src="https://queue.simpleanalyticscdn.com/noscript.gif"][alt=""][referrerpolicy="no-referrer-when-downgrade"]'; |
| 11 |
|
| 12 |
it('can be activated', function () { |
| 13 |
asAdmin() |
| 14 |
->navigate('http://localhost:8100/wp-admin/plugins.php') |
| 15 |
->assertPresent('tr[data-slug="simpleanalytics"]') |
| 16 |
->click('#activate-simpleanalytics') |
| 17 |
->assertPresent('a[href="options-general.php?page=simpleanalytics"]') |
| 18 |
->assertPresent('#deactivate-simpleanalytics') |
| 19 |
->screenshot(); |
| 20 |
}); |
| 21 |
|
| 22 |
it('adds a script by default', function () { |
| 23 |
visit('http://localhost:8100')->assertPresent(DEFAULT_SCRIPT_SELECTOR); |
| 24 |
}); |
| 25 |
|
| 26 |
it('adds inactive script for authenticated users by default', function () { |
| 27 |
asAdmin() |
| 28 |
->navigate('http://localhost:8100') |
| 29 |
->assertPresent('script[src="http://localhost:8100/wp-content/plugins/simpleanalytics/resources/js/inactive.js"]') |
| 30 |
->assertSourceHas(INACTIVE_ADMIN_COMMENT); |
| 31 |
}); |
| 32 |
|
| 33 |
it('adds a script with ignored pages', function () { |
| 34 |
asAdmin() |
| 35 |
->navigate('http://localhost:8100/wp-admin/options-general.php?page=simpleanalytics&tab=ignore-rules') |
| 36 |
->fill('simpleanalytics_ignore_pages', '/vouchers') |
| 37 |
->click('Save Changes') |
| 38 |
->refresh() |
| 39 |
->assertValue('simpleanalytics_ignore_pages', '/vouchers') |
| 40 |
->screenshot(); |
| 41 |
|
| 42 |
visit('http://localhost:8100')->refresh()->assertSourceHas('data-ignore-pages="/vouchers"'); |
| 43 |
}); |
| 44 |
|
| 45 |
it('adds inactive script for selected user roles', function () { |
| 46 |
$admin = asAdmin()->navigate('http://localhost:8100/wp-admin/options-general.php?page=simpleanalytics&tab=ignore-rules') |
| 47 |
->check('simpleanalytics_exclude_user_roles-editor') |
| 48 |
->check('simpleanalytics_exclude_user_roles-author') |
| 49 |
->click('Save Changes') |
| 50 |
->assertChecked('simpleanalytics_exclude_user_roles-editor') |
| 51 |
->assertChecked('simpleanalytics_exclude_user_roles-author'); |
| 52 |
|
| 53 |
$admin->navigate('http://localhost:8100') |
| 54 |
->assertPresent(DEFAULT_SCRIPT_SELECTOR); |
| 55 |
|
| 56 |
asAuthor()->navigate('http://localhost:8100') |
| 57 |
->assertPresent(INACTIVE_ADMIN_SCRIPT_SELECTOR) |
| 58 |
->assertSourceHas(INACTIVE_ADMIN_COMMENT); |
| 59 |
|
| 60 |
asEditor()->navigate('http://localhost:8100') |
| 61 |
->assertPresent(INACTIVE_ADMIN_SCRIPT_SELECTOR) |
| 62 |
->assertSourceHas(INACTIVE_ADMIN_COMMENT); |
| 63 |
}); |
| 64 |
|
| 65 |
it('adds a script with collect do not track enabled', function () { |
| 66 |
asAdmin()->navigate('http://localhost:8100/wp-admin/options-general.php?page=simpleanalytics&tab=advanced') |
| 67 |
->check('simpleanalytics_collect_dnt') |
| 68 |
->click('Save Changes') |
| 69 |
->assertChecked('simpleanalytics_collect_dnt'); |
| 70 |
|
| 71 |
visit('http://localhost:8100')->assertSourceHas('data-collect-dnt="true"'); |
| 72 |
}); |
| 73 |
|
| 74 |
it('adds a script with hash mode enabled', function () { |
| 75 |
asAdmin()->navigate('http://localhost:8100/wp-admin/options-general.php?page=simpleanalytics&tab=advanced') |
| 76 |
->check('simpleanalytics_hash_mode') |
| 77 |
->click('Save Changes') |
| 78 |
->assertChecked('simpleanalytics_hash_mode'); |
| 79 |
|
| 80 |
visit('http://localhost:8100')->assertSourceHas('data-mode="hash"'); |
| 81 |
}); |
| 82 |
|
| 83 |
it('adds a script with manually collect page views enabled', function () { |
| 84 |
asAdmin()->navigate('http://localhost:8100/wp-admin/options-general.php?page=simpleanalytics&tab=advanced') |
| 85 |
->check('simpleanalytics_manual_collect') |
| 86 |
->click('Save Changes') |
| 87 |
->assertChecked('simpleanalytics_manual_collect'); |
| 88 |
|
| 89 |
visit('http://localhost:8100')->assertSourceHas('data-auto-collect="true"'); |
| 90 |
}); |
| 91 |
|
| 92 |
/*it('adds noscript tag when support no javascript mode is enabled', function () { |
| 93 |
asAdmin()->navigate('http://localhost:8100/wp-admin/options-general.php?page=simpleanalytics&tab=advanced') |
| 94 |
->check('simpleanalytics_noscript') |
| 95 |
->click('Save Changes') |
| 96 |
->assertChecked('simpleanalytics_noscript'); |
| 97 |
|
| 98 |
visit('http://localhost:8100')->assertPresent(NOSCRIPT_SELECTOR); |
| 99 |
});*/ |
| 100 |
|
| 101 |
/*it('adds a script with onload callback', function () { |
| 102 |
asAdmin()->navigate('http://localhost:8100/wp-admin/options-general.php?page=simpleanalytics&tab=advanced') |
| 103 |
->fill('simpleanalytics_onload_callback', 'sa_event("My event")') |
| 104 |
->click('Save Changes') |
| 105 |
->assertValue('simpleanalytics_onload_callback', 'sa_event("My event")'); |
| 106 |
|
| 107 |
visit('http://localhost:8100')->assertSourceHas('data-onload="sa_event(\"My event\")"'); |
| 108 |
});*/ |
| 109 |
|
| 110 |
it('adds a script with overwrite domain name', function () { |
| 111 |
asAdmin()->navigate('http://localhost:8100/wp-admin/options-general.php?page=simpleanalytics&tab=advanced') |
| 112 |
->fill('simpleanalytics_hostname', 'example.com') |
| 113 |
->click('Save Changes') |
| 114 |
->assertValue('simpleanalytics_hostname', 'example.com'); |
| 115 |
|
| 116 |
visit('http://localhost:8100')->assertSourceHas('data-hostname="example.com"'); |
| 117 |
}); |
| 118 |
|
| 119 |
it('adds a script with global variable name', function () { |
| 120 |
asAdmin()->navigate('http://localhost:8100/wp-admin/options-general.php?page=simpleanalytics&tab=advanced') |
| 121 |
->fill('simpleanalytics_sa_global', 'ba_event') |
| 122 |
->click('Save Changes') |
| 123 |
->assertValue('simpleanalytics_sa_global', 'ba_event'); |
| 124 |
|
| 125 |
visit('http://localhost:8100')->assertSourceHas('data-sa-global="ba_event"'); |
| 126 |
}); |
| 127 |
|
| 128 |
it('adds automated events script when collect automated events is enabled', function () { |
| 129 |
asAdmin()->navigate('http://localhost:8100/wp-admin/options-general.php?page=simpleanalytics&tab=events') |
| 130 |
->check('simpleanalytics_automated_events') |
| 131 |
->click('Save Changes') |
| 132 |
->assertChecked('simpleanalytics_automated_events'); |
| 133 |
|
| 134 |
visit('http://localhost:8100')->assertPresent('script[src="https://scripts.simpleanalyticscdn.com/auto-events.js"]'); |
| 135 |
}); |
| 136 |
|
| 137 |
it('adds automated events script with auto collect downloads', function () { |
| 138 |
asAdmin()->navigate('http://localhost:8100/wp-admin/options-general.php?page=simpleanalytics&tab=events') |
| 139 |
->check('simpleanalytics_automated_events') |
| 140 |
->fill('simpleanalytics_event_collect_downloads', 'outbound,emails,downloads') |
| 141 |
->click('Save Changes') |
| 142 |
->assertChecked('simpleanalytics_automated_events') |
| 143 |
->assertValue('simpleanalytics_event_collect_downloads', 'outbound,emails,downloads'); |
| 144 |
|
| 145 |
visit('http://localhost:8100')->assertSourceHas('data-collect="outbound,emails,downloads"'); |
| 146 |
}); |
| 147 |
|
| 148 |
it('adds automated events script with download file extensions', function () { |
| 149 |
asAdmin()->navigate('http://localhost:8100/wp-admin/options-general.php?page=simpleanalytics&tab=events') |
| 150 |
->check('simpleanalytics_automated_events') |
| 151 |
->fill('simpleanalytics_event_extensions', 'pdf,zip') |
| 152 |
->click('Save Changes') |
| 153 |
->assertChecked('simpleanalytics_automated_events') |
| 154 |
->assertValue('simpleanalytics_event_extensions', 'pdf,zip'); |
| 155 |
|
| 156 |
visit('http://localhost:8100')->assertSourceHas('data-extensions="pdf,zip"'); |
| 157 |
}); |
| 158 |
|
| 159 |
it('adds automated events script with use titles of page enabled', function () { |
| 160 |
asAdmin()->navigate('http://localhost:8100/wp-admin/options-general.php?page=simpleanalytics&tab=events') |
| 161 |
->check('simpleanalytics_automated_events') |
| 162 |
->check('simpleanalytics_event_use_title') |
| 163 |
->click('Save Changes') |
| 164 |
->assertChecked('simpleanalytics_automated_events') |
| 165 |
->assertChecked('simpleanalytics_event_use_title'); |
| 166 |
|
| 167 |
visit('http://localhost:8100')->assertSourceHas('data-use-title'); |
| 168 |
}); |
| 169 |
|
| 170 |
it('adds automated events script with use full urls enabled', function () { |
| 171 |
asAdmin()->navigate('http://localhost:8100/wp-admin/options-general.php?page=simpleanalytics&tab=events') |
| 172 |
->check('simpleanalytics_automated_events') |
| 173 |
->check('simpleanalytics_event_full_urls') |
| 174 |
->click('Save Changes') |
| 175 |
->assertChecked('simpleanalytics_automated_events') |
| 176 |
->assertChecked('simpleanalytics_event_full_urls'); |
| 177 |
|
| 178 |
visit('http://localhost:8100')->assertSourceHas('data-full-urls'); |
| 179 |
}); |
| 180 |
|
| 181 |
it('adds automated events script with override global', function () { |
| 182 |
asAdmin()->navigate('http://localhost:8100/wp-admin/options-general.php?page=simpleanalytics&tab=events') |
| 183 |
->check('simpleanalytics_automated_events') |
| 184 |
->fill('simpleanalytics_event_sa_global', 'ba_event') |
| 185 |
->click('Save Changes') |
| 186 |
->assertChecked('simpleanalytics_automated_events') |
| 187 |
->assertValue('simpleanalytics_event_sa_global', 'ba_event'); |
| 188 |
|
| 189 |
visit('http://localhost:8100')->assertSourceHas('data-sa-global="ba_event"'); |
| 190 |
}); |
| 191 |
|
| 192 |
it('adds a script with a custom domain name', function () { |
| 193 |
asAdmin() |
| 194 |
->navigate('http://localhost:8100/wp-admin/options-general.php?page=simpleanalytics&tab=general') |
| 195 |
->fill('simpleanalytics_custom_domain', 'mydomain.com') |
| 196 |
->click('Save Changes') |
| 197 |
->assertValue('simpleanalytics_custom_domain', 'mydomain.com'); |
| 198 |
|
| 199 |
visit('http://localhost:8100')->assertPresent('script[src="https://mydomain.com/latest.js"]'); |
| 200 |
}); |
| 201 |
|