Skip to content

Commit 90b39a6

Browse files
Tests: Add case for wp_privacy_delete_old_export_files().
Props allendav. Merges [43292] to the 4.9 branch. See #43546. git-svn-id: https://develop.svn.wordpress.org/branches/4.9@43613 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 1da5a8b commit 90b39a6

File tree

1 file changed

+146
-0
lines changed

1 file changed

+146
-0
lines changed
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
<?php
2+
/**
3+
* Define a class to test `wp_privacy_delete_old_export_files()`.
4+
*
5+
* @package WordPress
6+
* @subpackage UnitTests
7+
* @since 4.9.6
8+
*/
9+
10+
/**
11+
* Test cases for `wp_privacy_delete_old_export_files()`.
12+
*
13+
* @group privacy
14+
* @covers wp_privacy_delete_old_export_files
15+
*
16+
* @since 4.9.6
17+
*/
18+
class Tests_Privacy_WpPrivacyDeleteOldExportFiles extends WP_UnitTestCase {
19+
/**
20+
* Path to the index file that blocks directory listing on poorly-configured servers.
21+
*
22+
* @since 4.9.6
23+
*
24+
* @var string $index_path
25+
*/
26+
protected static $index_path;
27+
28+
/**
29+
* Path to an export file that is past the expiration date.
30+
*
31+
* @since 4.9.6
32+
*
33+
* @var string $expired_export_file
34+
*/
35+
protected static $expired_export_file;
36+
37+
/**
38+
* Path to an export file that is active.
39+
*
40+
* @since 4.9.6
41+
*
42+
* @var string $expired_export_file
43+
*/
44+
protected static $active_export_file;
45+
46+
/**
47+
* Create fixtures that are shared by multiple test cases.
48+
*
49+
* @param WP_UnitTest_Factory $factory The base factory object.
50+
*/
51+
public static function wpSetUpBeforeClass( $factory ) {
52+
$exports_dir = wp_privacy_exports_dir();
53+
54+
if ( ! is_dir( $exports_dir ) ) {
55+
wp_mkdir_p( $exports_dir );
56+
}
57+
58+
self::$index_path = $exports_dir . 'index.html';
59+
self::$expired_export_file = $exports_dir . 'wp-personal-data-file-user-at-example-com-0123456789abcdef.zip';
60+
self::$active_export_file = $exports_dir . 'wp-personal-data-file-user-at-example-com-fedcba9876543210.zip';
61+
}
62+
63+
/**
64+
* Perform setup operations that are shared across all tests.
65+
*/
66+
public function setUp() {
67+
parent::setUp();
68+
69+
touch( self::$index_path, time() - 30 * WEEK_IN_SECONDS );
70+
touch( self::$expired_export_file, time() - 5 * DAY_IN_SECONDS );
71+
touch( self::$active_export_file, time() - 2 * DAY_IN_SECONDS );
72+
}
73+
74+
/**
75+
* Restore the system state to what it was before this case was setup.
76+
*/
77+
public static function wpTearDownAfterClass() {
78+
wp_delete_file( self::$expired_export_file );
79+
wp_delete_file( self::$active_export_file );
80+
}
81+
82+
/**
83+
* The function should not throw notices when the exports directory doesn't exist.
84+
*
85+
* @since 4.9.6
86+
*/
87+
public function test_non_existent_folders_should_not_cause_errors() {
88+
add_filter( 'wp_privacy_exports_dir', array( $this, 'filter_bad_exports_dir' ) );
89+
wp_privacy_delete_old_export_files();
90+
remove_filter( 'wp_privacy_exports_dir', array( $this, 'filter_bad_exports_dir' ) );
91+
92+
/*
93+
* The test will automatically fail if the function triggers a notice,
94+
* so this dummy assertion is just for accurate stats.
95+
*/
96+
$this->assertTrue( true );
97+
}
98+
99+
/**
100+
* Return the path to a non-existent folder.
101+
*
102+
* @since 4.9.6
103+
*
104+
* @param string $exports_dir The default personal data export directory.
105+
*
106+
* @return string The path to a folder that doesn't exist.
107+
*/
108+
public function filter_bad_exports_dir( $exports_dir ) {
109+
$upload_dir = wp_upload_dir();
110+
111+
return trailingslashit( $upload_dir['basedir'] ) . 'invalid-12345';
112+
}
113+
114+
/**
115+
* The function should delete files that are past the expiration date.
116+
*
117+
* @since 4.9.6
118+
*/
119+
public function test_expired_files_should_be_deleted() {
120+
wp_privacy_delete_old_export_files();
121+
122+
$this->assertFalse( file_exists( self::$expired_export_file ) );
123+
}
124+
125+
/**
126+
* The function should not delete files that are not past the expiration date.
127+
*
128+
* @since 4.9.6
129+
*/
130+
public function test_unexpired_files_should_not_be_deleted() {
131+
wp_privacy_delete_old_export_files();
132+
133+
$this->assertTrue( file_exists( self::$active_export_file ) );
134+
}
135+
136+
/**
137+
* The function should never delete the index file, even if it's past the expiration date.
138+
*
139+
* @since 4.9.6
140+
*/
141+
public function test_index_file_should_never_be_deleted() {
142+
wp_privacy_delete_old_export_files();
143+
144+
$this->assertTrue( file_exists( self::$index_path ) );
145+
}
146+
}

0 commit comments

Comments
 (0)