forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.php
More file actions
155 lines (117 loc) · 4.34 KB
/
file.php
File metadata and controls
155 lines (117 loc) · 4.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
/**
* @group file
*/
class Tests_File extends WP_UnitTestCase {
function setUp() {
parent::setUp();
$this->dir = dirname(tempnam('/tmp', 'foo'));
$this->badchars = '"\'[]*&?$';
}
/**
* @group plugins
* @group themes
*/
function test_get_file_data() {
$theme_headers = array(
'Name' => 'Theme Name',
'ThemeURI' => 'Theme URI',
'Description' => 'Description',
'Version' => 'Version',
'Author' => 'Author',
'AuthorURI' => 'Author URI',
);
$actual = get_file_data( DIR_TESTDATA . '/themedir1/default/style.css', $theme_headers );
$expected = array(
'Name' => 'WordPress Default',
'ThemeURI' => 'http://wordpress.org/',
'Description' => 'The default WordPress theme based on the famous <a href="http://binarybonsai.com/kubrick/">Kubrick</a>.',
'Version' => '1.6',
'Author' => 'Michael Heilemann',
'AuthorURI' => 'http://binarybonsai.com/',
);
foreach ( $actual as $header => $value )
$this->assertEquals( $expected[ $header ], $value, $header );
}
/**
* @group plugins
* @group themes
*/
function test_get_file_data_cr_line_endings() {
$headers = array( 'SomeHeader' => 'Some Header', 'Description' => 'Description', 'Author' => 'Author' );
$actual = get_file_data( DIR_TESTDATA . '/formatting/cr-line-endings-file-header.php', $headers );
$expected = array(
'SomeHeader' => 'Some header value!',
'Description' => 'This file is using CR line endings for a testcase.',
'Author' => 'A Very Old Mac',
);
foreach ( $actual as $header => $value )
$this->assertEquals( $expected[ $header ], $value, $header );
}
function is_unique_writable_file($path, $filename) {
$fullpath = $path . DIRECTORY_SEPARATOR . $filename;
$fp = fopen( $fullpath, 'x' );
// file already exists?
if (!$fp)
return false;
// write some random contents
$c = rand_str();
fwrite($fp, $c);
fclose($fp);
if ( file_get_contents($fullpath) === $c )
$result = true;
else
$result = false;
return $result;
}
function test_unique_filename_is_valid() {
// make sure it produces a valid, writable, unique filename
$filename = wp_unique_filename( $this->dir, rand_str() . '.txt' );
$this->assertTrue( $this->is_unique_writable_file($this->dir, $filename) );
unlink($this->dir . DIRECTORY_SEPARATOR . $filename);
}
function test_unique_filename_is_unique() {
// make sure it produces two unique filenames
$name = rand_str();
$filename1 = wp_unique_filename( $this->dir, $name . '.txt' );
$this->assertTrue( $this->is_unique_writable_file($this->dir, $filename1) );
$filename2 = wp_unique_filename( $this->dir, $name . '.txt' );
$this->assertTrue( $this->is_unique_writable_file($this->dir, $filename2) );
// the two should be different
$this->assertNotEquals( $filename1, $filename2 );
unlink($this->dir . DIRECTORY_SEPARATOR . $filename1);
unlink($this->dir . DIRECTORY_SEPARATOR . $filename2);
}
function test_unique_filename_is_sanitized() {
$name = rand_str();
$filename = wp_unique_filename( $this->dir, $name . $this->badchars . '.txt' );
// make sure the bad characters were all stripped out
$this->assertEquals( $name . '.txt', $filename );
$this->assertTrue( $this->is_unique_writable_file($this->dir, $filename) );
unlink($this->dir . DIRECTORY_SEPARATOR . $filename);
}
function test_unique_filename_with_slashes() {
$name = rand_str();
// "foo/foo.txt"
$filename = wp_unique_filename( $this->dir, $name . '/' . $name . '.txt' );
// the slash should be removed, i.e. "foofoo.txt"
$this->assertEquals( $name . $name . '.txt', $filename );
$this->assertTrue( $this->is_unique_writable_file($this->dir, $filename) );
unlink($this->dir . DIRECTORY_SEPARATOR . $filename);
}
function test_unique_filename_multiple_ext() {
$name = rand_str();
$filename = wp_unique_filename( $this->dir, $name . '.php.txt' );
// "foo.php.txt" becomes "foo.php_.txt"
$this->assertEquals( $name . '.php_.txt', $filename );
$this->assertTrue( $this->is_unique_writable_file($this->dir, $filename) );
unlink($this->dir . DIRECTORY_SEPARATOR . $filename);
}
function test_unique_filename_no_ext() {
$name = rand_str();
$filename = wp_unique_filename( $this->dir, $name );
$this->assertEquals( $name, $filename );
$this->assertTrue( $this->is_unique_writable_file($this->dir, $filename) );
unlink($this->dir . DIRECTORY_SEPARATOR . $filename);
}
}