Skip to content

Commit 8af66e2

Browse files
committed
Add path params to wordpress
1 parent 723655a commit 8af66e2

File tree

11 files changed

+324
-9
lines changed

11 files changed

+324
-9
lines changed

src/Integrations/Integrations/WordPress/WordPressIntegration.php

+13
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,19 @@ public function init()
7070
return true;
7171
});
7272

73+
\DDTrace\hook_method('WP', 'main', null, function ($This, $scope, $args) {
74+
if (\property_exists($This, 'did_permalink') && $This->did_permalink === true) {
75+
if (function_exists('\datadog\appsec\push_params') &&
76+
\property_exists($This, 'query_vars') &&
77+
function_exists('is_404') && is_404() === false) {
78+
$parameters = $This->query_vars;
79+
if (count($parameters) > 0) {
80+
\datadog\appsec\push_params($parameters);
81+
}
82+
}
83+
}
84+
});
85+
7386
\DDTrace\hook_function(
7487
'wp_authenticate',
7588
null,

tests/Frameworks/WordPress/Version_4_8/wp-config.php

+3
Original file line numberDiff line numberDiff line change
@@ -89,5 +89,8 @@
8989
if ( !defined('ABSPATH') )
9090
define('ABSPATH', dirname(__FILE__) . '/');
9191

92+
//Appsec mock. This wont be needed on customer apps since this functions will be exposed by appsec
93+
require __DIR__.'/../../../Appsec/Mock.php';
94+
9295
/** Sets up WordPress vars and included files. */
9396
require_once(ABSPATH . 'wp-settings.php');

tests/Frameworks/WordPress/Version_4_8/wp-login.php

-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@
1111
/** Make sure that the WordPress bootstrap has run before continuing. */
1212
require( dirname(__FILE__) . '/wp-load.php' );
1313

14-
//Appsec mock. This wont be needed on customer apps since this functions will be exposed by appsec
15-
require __DIR__.'/../../../Appsec/Mock.php';
16-
1714
// Redirect to https login if forced to use SSL
1815
if ( force_ssl_admin() && ! is_ssl() ) {
1916
if ( 0 === strpos($_SERVER['REQUEST_URI'], 'http') ) {

tests/Frameworks/WordPress/Version_5_5/wp-config.php

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/Frameworks/WordPress/Version_5_5/wp-login.php

-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/Frameworks/WordPress/Version_5_9/wp-config.php

+3
Original file line numberDiff line numberDiff line change
@@ -94,5 +94,8 @@
9494
define( 'ABSPATH', __DIR__ . '/' );
9595
}
9696

97+
//Appsec mock. This wont be needed on customer apps since this functions will be exposed by appsec
98+
require __DIR__.'/../../../Appsec/Mock.php';
99+
97100
/** Sets up WordPress vars and included files. */
98101
require_once ABSPATH . 'wp-settings.php';

tests/Frameworks/WordPress/Version_5_9/wp-login.php

-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@
1111
/** Make sure that the WordPress bootstrap has run before continuing. */
1212
require __DIR__ . '/wp-load.php';
1313

14-
//Appsec mock. This wont be needed on customer apps since this functions will be exposed by appsec
15-
require __DIR__.'/../../../Appsec/Mock.php';
16-
1714
// Redirect to HTTPS login if forced to use SSL.
1815
if ( force_ssl_admin() && ! is_ssl() ) {
1916
if ( 0 === strpos( $_SERVER['REQUEST_URI'], 'http' ) ) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
namespace DDTrace\Tests\Integrations\WordPress\V5_9;
4+
5+
use DDTrace\Tests\Common\WebFrameworkTestCase;
6+
use DDTrace\Tests\Frameworks\Util\Request\GetSpec;
7+
use datadog\appsec\AppsecStatus;
8+
9+
class PathParamsTest extends WebFrameworkTestCase
10+
{
11+
protected static function getAppIndexScript()
12+
{
13+
return __DIR__ . '/../../../Frameworks/WordPress/Version_4_8/index.php';
14+
}
15+
16+
protected function connection()
17+
{
18+
return new \PDO('mysql:host=mysql_integration;dbname=test', 'test', 'test');
19+
}
20+
21+
protected function ddSetUp()
22+
{
23+
parent::ddSetUp();
24+
$this->connection()->exec(file_get_contents(__DIR__ . '/../../../Frameworks/WordPress/Version_5_5/wp_2020-10-21.sql'));
25+
AppsecStatus::getInstance()->setDefaults();
26+
}
27+
28+
public static function ddSetUpBeforeClass()
29+
{
30+
parent::ddSetUpBeforeClass();
31+
AppsecStatus::getInstance()->init();
32+
}
33+
34+
public static function ddTearDownAfterClass()
35+
{
36+
AppsecStatus::getInstance()->destroy();
37+
parent::ddTearDownAfterClass();
38+
}
39+
40+
public function testPost()
41+
{
42+
$this->call(
43+
GetSpec::create(
44+
'Post example',
45+
'/hello-world'
46+
)
47+
);
48+
49+
$events = AppsecStatus::getInstance()->getEvents();
50+
$this->assertEquals(1, count($events));
51+
$this->assertEquals('hello-world', $events[0]['name']);
52+
$this->assertEquals('push_params', $events[0]['eventName']);
53+
}
54+
55+
public function testCategory()
56+
{
57+
$this->call(
58+
GetSpec::create(
59+
'Category',
60+
'/category/uncategorized'
61+
)
62+
);
63+
64+
$events = AppsecStatus::getInstance()->getEvents();
65+
$this->assertEquals(1, count($events));
66+
$this->assertEquals('uncategorized', $events[0]['category_name']);
67+
$this->assertEquals('push_params', $events[0]['eventName']);
68+
}
69+
70+
public function testAuthor()
71+
{
72+
$this->call(
73+
GetSpec::create(
74+
'Author',
75+
'/author/test'
76+
)
77+
);
78+
79+
$events = AppsecStatus::getInstance()->getEvents();
80+
$this->assertEquals(1, count($events));
81+
$this->assertEquals('test', $events[0]['author_name']);
82+
$this->assertEquals('push_params', $events[0]['eventName']);
83+
}
84+
85+
public function testNonExistingPost()
86+
{
87+
$this->call(
88+
GetSpec::create(
89+
'Not existing post',
90+
'/non-existing-post'
91+
)
92+
);
93+
94+
$events = AppsecStatus::getInstance()->getEvents();
95+
$this->assertEquals(0, count($events));
96+
}
97+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
namespace DDTrace\Tests\Integrations\WordPress\V5_9;
4+
5+
use DDTrace\Tests\Common\WebFrameworkTestCase;
6+
use DDTrace\Tests\Frameworks\Util\Request\GetSpec;
7+
use datadog\appsec\AppsecStatus;
8+
9+
class PathParamsTest extends WebFrameworkTestCase
10+
{
11+
protected static function getAppIndexScript()
12+
{
13+
return __DIR__ . '/../../../Frameworks/WordPress/Version_5_5/index.php';
14+
}
15+
16+
protected function connection()
17+
{
18+
return new \PDO('mysql:host=mysql_integration;dbname=test', 'test', 'test');
19+
}
20+
21+
protected function ddSetUp()
22+
{
23+
parent::ddSetUp();
24+
$this->connection()->exec(file_get_contents(__DIR__ . '/../../../Frameworks/WordPress/Version_5_5/wp_2020-10-21.sql'));
25+
AppsecStatus::getInstance()->setDefaults();
26+
}
27+
28+
public static function ddSetUpBeforeClass()
29+
{
30+
parent::ddSetUpBeforeClass();
31+
AppsecStatus::getInstance()->init();
32+
}
33+
34+
public static function ddTearDownAfterClass()
35+
{
36+
AppsecStatus::getInstance()->destroy();
37+
parent::ddTearDownAfterClass();
38+
}
39+
40+
public function testPost()
41+
{
42+
$this->call(
43+
GetSpec::create(
44+
'Post example',
45+
'/hello-world'
46+
)
47+
);
48+
49+
$events = AppsecStatus::getInstance()->getEvents();
50+
$this->assertEquals(1, count($events));
51+
$this->assertEquals('hello-world', $events[0]['name']);
52+
$this->assertEquals('push_params', $events[0]['eventName']);
53+
}
54+
55+
public function testCategory()
56+
{
57+
$this->call(
58+
GetSpec::create(
59+
'Category',
60+
'/category/uncategorized'
61+
)
62+
);
63+
64+
$events = AppsecStatus::getInstance()->getEvents();
65+
$this->assertEquals(1, count($events));
66+
$this->assertEquals('uncategorized', $events[0]['category_name']);
67+
$this->assertEquals('push_params', $events[0]['eventName']);
68+
}
69+
70+
public function testAuthor()
71+
{
72+
$this->call(
73+
GetSpec::create(
74+
'Author',
75+
'/author/test'
76+
)
77+
);
78+
79+
$events = AppsecStatus::getInstance()->getEvents();
80+
$this->assertEquals(1, count($events));
81+
$this->assertEquals('test', $events[0]['author_name']);
82+
$this->assertEquals('push_params', $events[0]['eventName']);
83+
}
84+
85+
public function testNonExistingPost()
86+
{
87+
$this->call(
88+
GetSpec::create(
89+
'Not existing post',
90+
'/non-existing-post'
91+
)
92+
);
93+
94+
$events = AppsecStatus::getInstance()->getEvents();
95+
$this->assertEquals(0, count($events));
96+
}
97+
}

tests/Integrations/WordPress/V5_9/AutomatedLoginEventsTest.php

+11
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use DDTrace\Tests\Common\WebFrameworkTestCase;
66
use DDTrace\Tests\Frameworks\Util\Request\PostSpec;
7+
use DDTrace\Tests\Frameworks\Util\Request\GetSpec;
78
use datadog\appsec\AppsecStatus;
89

910
class AutomatedLoginEventsTest extends WebFrameworkTestCase
@@ -137,4 +138,14 @@ public function testUserSignUp()
137138
$this->assertEquals($users[0]['user_login'], $signUpEvent['metadata']['username']);
138139
$this->assertEquals($users[0]['user_email'], $signUpEvent['metadata']['email']);
139140
}
141+
142+
public function testAlex()
143+
{
144+
var_dump($this->call(
145+
GetSpec::create(
146+
'A simple GET request with a view',
147+
'/hello-world'
148+
)
149+
));
150+
}
140151
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
namespace DDTrace\Tests\Integrations\WordPress\V5_9;
4+
5+
use DDTrace\Tests\Common\WebFrameworkTestCase;
6+
use DDTrace\Tests\Frameworks\Util\Request\GetSpec;
7+
use datadog\appsec\AppsecStatus;
8+
9+
class PathParamsTest extends WebFrameworkTestCase
10+
{
11+
protected static function getAppIndexScript()
12+
{
13+
return __DIR__ . '/../../../Frameworks/WordPress/Version_5_9/index.php';
14+
}
15+
16+
protected function connection()
17+
{
18+
return new \PDO('mysql:host=mysql_integration;dbname=test', 'test', 'test');
19+
}
20+
21+
protected function ddSetUp()
22+
{
23+
parent::ddSetUp();
24+
$this->connection()->exec(file_get_contents(__DIR__ . '/../../../Frameworks/WordPress/Version_5_5/wp_2020-10-21.sql'));
25+
AppsecStatus::getInstance()->setDefaults();
26+
}
27+
28+
public static function ddSetUpBeforeClass()
29+
{
30+
parent::ddSetUpBeforeClass();
31+
AppsecStatus::getInstance()->init();
32+
}
33+
34+
public static function ddTearDownAfterClass()
35+
{
36+
AppsecStatus::getInstance()->destroy();
37+
parent::ddTearDownAfterClass();
38+
}
39+
40+
public function testPost()
41+
{
42+
$this->call(
43+
GetSpec::create(
44+
'Post example',
45+
'/hello-world'
46+
)
47+
);
48+
49+
$events = AppsecStatus::getInstance()->getEvents();
50+
$this->assertEquals(1, count($events));
51+
$this->assertEquals('hello-world', $events[0]['name']);
52+
$this->assertEquals('push_params', $events[0]['eventName']);
53+
}
54+
55+
public function testCategory()
56+
{
57+
$this->call(
58+
GetSpec::create(
59+
'Category',
60+
'/category/uncategorized'
61+
)
62+
);
63+
64+
$events = AppsecStatus::getInstance()->getEvents();
65+
$this->assertEquals(1, count($events));
66+
$this->assertEquals('uncategorized', $events[0]['category_name']);
67+
$this->assertEquals('push_params', $events[0]['eventName']);
68+
}
69+
70+
public function testAuthor()
71+
{
72+
$this->call(
73+
GetSpec::create(
74+
'Author',
75+
'/author/test'
76+
)
77+
);
78+
79+
$events = AppsecStatus::getInstance()->getEvents();
80+
$this->assertEquals(1, count($events));
81+
$this->assertEquals('test', $events[0]['author_name']);
82+
$this->assertEquals('push_params', $events[0]['eventName']);
83+
}
84+
85+
public function testNonExistingPost()
86+
{
87+
$this->call(
88+
GetSpec::create(
89+
'Not existing post',
90+
'/non-existing-post'
91+
)
92+
);
93+
94+
$events = AppsecStatus::getInstance()->getEvents();
95+
$this->assertEquals(0, count($events));
96+
}
97+
}

0 commit comments

Comments
 (0)