Changeset 42343 for trunk/tests/phpunit/tests/file.php
- Timestamp:
- 11/30/2017 11:09:33 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/file.php
r38762 r42343 9 9 parent::setUp(); 10 10 11 $file = tempnam( '/tmp', 'foo');11 $file = tempnam( '/tmp', 'foo' ); 12 12 $this->dir = dirname( $file ); 13 13 unlink( $file ); … … 22 22 function test_get_file_data() { 23 23 $theme_headers = array( 24 'Name' => 'Theme Name',25 'ThemeURI' => 'Theme URI',24 'Name' => 'Theme Name', 25 'ThemeURI' => 'Theme URI', 26 26 'Description' => 'Description', 27 'Version' => 'Version',28 'Author' => 'Author',29 'AuthorURI' => 'Author URI',27 'Version' => 'Version', 28 'Author' => 'Author', 29 'AuthorURI' => 'Author URI', 30 30 ); 31 31 … … 33 33 34 34 $expected = array( 35 'Name' => 'WordPress Default',36 'ThemeURI' => 'http://wordpress.org/',35 'Name' => 'WordPress Default', 36 'ThemeURI' => 'http://wordpress.org/', 37 37 'Description' => 'The default WordPress theme based on the famous <a href="http://binarybonsai.com/kubrick/">Kubrick</a>.', 38 'Version' => '1.6',39 'Author' => 'Michael Heilemann',40 'AuthorURI' => 'http://binarybonsai.com/',38 'Version' => '1.6', 39 'Author' => 'Michael Heilemann', 40 'AuthorURI' => 'http://binarybonsai.com/', 41 41 ); 42 42 43 foreach ( $actual as $header => $value ) 43 foreach ( $actual as $header => $value ) { 44 44 $this->assertEquals( $expected[ $header ], $value, $header ); 45 } 45 46 } 46 47 … … 50 51 */ 51 52 function test_get_file_data_cr_line_endings() { 52 $headers = array( 'SomeHeader' => 'Some Header', 'Description' => 'Description', 'Author' => 'Author' ); 53 $actual = get_file_data( DIR_TESTDATA . '/formatting/cr-line-endings-file-header.php', $headers ); 53 $headers = array( 54 'SomeHeader' => 'Some Header', 55 'Description' => 'Description', 56 'Author' => 'Author', 57 ); 58 $actual = get_file_data( DIR_TESTDATA . '/formatting/cr-line-endings-file-header.php', $headers ); 54 59 $expected = array( 55 'SomeHeader' => 'Some header value!',60 'SomeHeader' => 'Some header value!', 56 61 'Description' => 'This file is using CR line endings for a testcase.', 57 'Author' => 'A Very Old Mac',62 'Author' => 'A Very Old Mac', 58 63 ); 59 64 60 foreach ( $actual as $header => $value ) 65 foreach ( $actual as $header => $value ) { 61 66 $this->assertEquals( $expected[ $header ], $value, $header ); 67 } 62 68 } 63 69 64 function is_unique_writable_file( $path, $filename) {70 function is_unique_writable_file( $path, $filename ) { 65 71 $fullpath = $path . DIRECTORY_SEPARATOR . $filename; 66 72 67 73 $fp = fopen( $fullpath, 'x' ); 68 74 // file already exists? 69 if ( !$fp)75 if ( ! $fp ) { 70 76 return false; 77 } 71 78 72 79 // write some random contents 73 80 $c = rand_str(); 74 fwrite( $fp, $c);75 fclose( $fp);81 fwrite( $fp, $c ); 82 fclose( $fp ); 76 83 77 if ( file_get_contents( $fullpath) === $c )84 if ( file_get_contents( $fullpath ) === $c ) { 78 85 $result = true; 79 else86 } else { 80 87 $result = false; 88 } 81 89 82 90 return $result; … … 87 95 $filename = wp_unique_filename( $this->dir, __FUNCTION__ . '.txt' ); 88 96 89 $this->assertTrue( $this->is_unique_writable_file( $this->dir, $filename) );97 $this->assertTrue( $this->is_unique_writable_file( $this->dir, $filename ) ); 90 98 91 unlink( $this->dir . DIRECTORY_SEPARATOR . $filename);99 unlink( $this->dir . DIRECTORY_SEPARATOR . $filename ); 92 100 } 93 101 … … 97 105 98 106 $filename1 = wp_unique_filename( $this->dir, $name . '.txt' ); 99 $this->assertTrue( $this->is_unique_writable_file( $this->dir, $filename1) );107 $this->assertTrue( $this->is_unique_writable_file( $this->dir, $filename1 ) ); 100 108 $filename2 = wp_unique_filename( $this->dir, $name . '.txt' ); 101 $this->assertTrue( $this->is_unique_writable_file( $this->dir, $filename2) );109 $this->assertTrue( $this->is_unique_writable_file( $this->dir, $filename2 ) ); 102 110 103 111 // the two should be different 104 112 $this->assertNotEquals( $filename1, $filename2 ); 105 113 106 unlink( $this->dir . DIRECTORY_SEPARATOR . $filename1);107 unlink( $this->dir . DIRECTORY_SEPARATOR . $filename2);114 unlink( $this->dir . DIRECTORY_SEPARATOR . $filename1 ); 115 unlink( $this->dir . DIRECTORY_SEPARATOR . $filename2 ); 108 116 } 109 117 110 118 function test_unique_filename_is_sanitized() { 111 $name = __FUNCTION__;112 $filename = wp_unique_filename( $this->dir, $name . $this->badchars . 119 $name = __FUNCTION__; 120 $filename = wp_unique_filename( $this->dir, $name . $this->badchars . '.txt' ); 113 121 114 122 // make sure the bad characters were all stripped out 115 123 $this->assertEquals( $name . '.txt', $filename ); 116 124 117 $this->assertTrue( $this->is_unique_writable_file( $this->dir, $filename) );125 $this->assertTrue( $this->is_unique_writable_file( $this->dir, $filename ) ); 118 126 119 unlink( $this->dir . DIRECTORY_SEPARATOR . $filename);127 unlink( $this->dir . DIRECTORY_SEPARATOR . $filename ); 120 128 } 121 129 … … 123 131 $name = __FUNCTION__; 124 132 // "foo/foo.txt" 125 $filename = wp_unique_filename( $this->dir, $name . '/' . $name . 133 $filename = wp_unique_filename( $this->dir, $name . '/' . $name . '.txt' ); 126 134 127 135 // the slash should be removed, i.e. "foofoo.txt" 128 136 $this->assertEquals( $name . $name . '.txt', $filename ); 129 137 130 $this->assertTrue( $this->is_unique_writable_file( $this->dir, $filename) );138 $this->assertTrue( $this->is_unique_writable_file( $this->dir, $filename ) ); 131 139 132 unlink( $this->dir . DIRECTORY_SEPARATOR . $filename);140 unlink( $this->dir . DIRECTORY_SEPARATOR . $filename ); 133 141 } 134 142 135 143 function test_unique_filename_multiple_ext() { 136 $name = __FUNCTION__;144 $name = __FUNCTION__; 137 145 $filename = wp_unique_filename( $this->dir, $name . '.php.txt' ); 138 146 … … 140 148 $this->assertEquals( $name . '.php_.txt', $filename ); 141 149 142 $this->assertTrue( $this->is_unique_writable_file( $this->dir, $filename) );150 $this->assertTrue( $this->is_unique_writable_file( $this->dir, $filename ) ); 143 151 144 unlink( $this->dir . DIRECTORY_SEPARATOR . $filename);152 unlink( $this->dir . DIRECTORY_SEPARATOR . $filename ); 145 153 } 146 154 147 155 function test_unique_filename_no_ext() { 148 $name = __FUNCTION__;156 $name = __FUNCTION__; 149 157 $filename = wp_unique_filename( $this->dir, $name ); 150 158 151 159 $this->assertEquals( $name, $filename ); 152 160 153 $this->assertTrue( $this->is_unique_writable_file( $this->dir, $filename) );161 $this->assertTrue( $this->is_unique_writable_file( $this->dir, $filename ) ); 154 162 155 unlink( $this->dir . DIRECTORY_SEPARATOR . $filename);163 unlink( $this->dir . DIRECTORY_SEPARATOR . $filename ); 156 164 } 157 165
Note: See TracChangeset
for help on using the changeset viewer.