WordPress.org

Make WordPress Core

Ticket #45615: 45615.5.diff

File 45615.5.diff, 12.3 KB (added by tellyworth, 10 months ago)

Rearrange tests, and add tests for adding unsupported types by filter

  • src/wp-includes/functions.php

     
    23732373                         * This means that common mismatches are forgiven: application/vnd.apple.numbers is often misidentified as application/zip,
    23742374                         * and some media files are commonly named with the wrong extension (.mov instead of .mp4)
    23752375                         */
    2376 
    23772376                        if ( substr( $real_mime, 0, strcspn( $real_mime, '/' ) ) !== substr( $type, 0, strcspn( $type, '/' ) ) ) {
    23782377                                $type = $ext = false;
    23792378                        }
     2379                } elseif ( 'text/plain' === $real_mime ) {
     2380                        // A few common file types are occasionally detected as text/plain; allow those.
     2381                        if ( ! in_array( $type, array( 'text/plain', 'text/csv', 'text/tsv', 'text/vtt' ) ) ) {
     2382                                $type = $ext = false;
     2383                        }
    23802384                } else {
    23812385                        if ( $type !== $real_mime ) {
    23822386                                /*
     
    24982502        'rtx' => 'text/richtext',
    24992503        'css' => 'text/css',
    25002504        'htm|html' => 'text/html',
    2501         'vtt' => 'text/vtt',
    2502         'dfxp' => 'application/ttaf+xml',
    25032505        // Audio formats.
    25042506        'mp3|m4a|m4b' => 'audio/mpeg',
    25052507        'aac' => 'audio/aac',
  • tests/phpunit/data/uploads/test.csv

     
     1"country","country group","name (en)","name (fr)","name (de)","latitude","longitude"
     2"at","eu","Austria","Autriche","Österreich","47.6965545","13.34598005"
     3"be","eu","Belgium","Belgique","Belgien","50.501045","4.47667405"
     4"bg","eu","Bulgaria","Bulgarie","Bulgarien","42.72567375","25.4823218"
     5"country","country group","name (en)","name (fr)","name (de)","latitude","longitude"
     6"at","eu","Austria","Autriche","Österreich","47.6965545","13.34598005"
     7"be","eu","Belgium","Belgique","Belgien","50.501045","4.47667405"
     8"bg","eu","Bulgaria","Bulgarie","Bulgarien","42.72567375","25.4823218"
     9 No newline at end of file
  • tests/phpunit/data/uploads/test.dfxp

     
     1<?xml version="1.0" encoding="utf-8"?>
     2<tt xml:lang="en" xmlns="http://www.w3.org/ns/ttml"
     3        xmlns:tts="http://www.w3.org/ns/ttml#styling"
     4        xmlns:ttm="http://www.w3.org/ns/ttml#metadata">
     5        <head>
     6                <styling>
     7                        <style xml:id="defaultCaption" tts:fontSize="10" tts:fontFamily="SansSerif"
     8                        tts:fontWeight="normal" tts:fontStyle="normal"
     9                        tts:textDecoration="none" tts:color="white"
     10                        tts:backgroundColor="black" />
     11                </styling>
     12
     13        </head>
     14        <body>
     15                <div style="defaultCaption" xml:lang="en">
     16                        <p begin="00:00:03.400" end="00:00:06.177">In this lesson, we're going to<br />be talking about finance. And</p>
     17                        <p begin="00:00:06.177" end="00:00:10.009">one of the most important aspects<br />of finance is interest.</p>
     18                        <p begin="00:00:10.009" end="00:00:13.655">When I go to a bank or some<br />other lending institution</p>
     19                        <p begin="00:00:13.655" end="00:00:17.720">to borrow money, the bank is happy<br />to give me that money. But then I'm</p>
     20                        <p begin="00:00:17.900" end="00:00:21.480">going to be paying the bank for the<br />privilege of using their money. And that</p>
     21                        <p begin="00:00:21.660" end="00:00:26.440">amount of money that I pay the bank is<br />called interest. Likewise, if I put money</p>
     22                        <p begin="00:00:26.620" end="00:00:31.220">in a savings account or I purchase a<br />certificate of deposit, the bank just</p>
     23                        <p begin="00:00:31.300" end="00:00:35.800">doesn't put my money in a little box<br />and leave it there until later. They take</p>
     24                        <p begin="00:00:35.800" end="00:00:40.822">my money and lend it to someone<br />else. So they are using my money.</p>
     25                        <p begin="00:00:40.822" end="00:00:44.400">The bank has to pay me for the privilege<br />of using my money.</p>
     26                        <p begin="00:00:44.400" end="00:00:48.700">Now what makes banks<br />profitable is the rate</p>
     27                        <p begin="00:00:48.700" end="00:00:53.330">that they charge people to use the bank's<br />money is higher than the rate that they</p>
     28                        <p begin="00:00:53.510" end="00:01:00.720">pay people like me to use my money. The<br />amount of interest that a person pays or</p>
     29                        <p begin="00:01:00.800" end="00:01:06.640">earns is dependent on three things. It's<br />dependent on how much money is involved.</p>
     30                        <p begin="00:01:06.820" end="00:01:11.300">It's dependent upon the rate of interest<br />being paid or the rate of interest being</p>
     31                        <p begin="00:01:11.480" end="00:01:17.898">charged. And it's also dependent upon<br />how much time is involved. If I have</p>
     32                        <p begin="00:01:17.898" end="00:01:22.730">a loan and I want to decrease the amount<br />of interest that I'm going to pay, then</p>
     33                        <p begin="00:01:22.800" end="00:01:28.040">I'm either going to have to decrease how<br />much money I borrow, I'm going to have</p>
     34                        <p begin="00:01:28.220" end="00:01:32.420">to borrow the money over a shorter period<br />of time, or I'm going to have to find a</p>
     35                        <p begin="00:01:32.600" end="00:01:37.279">lending institution that charges a lower<br />interest rate. On the other hand, if I</p>
     36                        <p begin="00:01:37.279" end="00:01:41.480">want to earn more interest on my<br />investment, I'm going to have to invest</p>
     37                        <p begin="00:01:41.480" end="00:01:46.860">more money, leave the money in the<br />account for a longer period of time, or</p>
     38                        <p begin="00:01:46.860" end="00:01:49.970">find an institution that will pay<br />me a higher interest rate.</p>
     39                </div>
     40        </body>
     41</tt>
  • tests/phpunit/data/uploads/test.json

     
     1{"menu": {
     2  "id": "file",
     3  "value": "File",
     4  "popup": {
     5    "menuitem": [
     6      {"value": "New", "onclick": "CreateNewDoc()"},
     7      {"value": "Open", "onclick": "OpenDoc()"},
     8      {"value": "Close", "onclick": "CloseDoc()"}
     9    ]
     10  }
     11}}
     12 No newline at end of file
  • tests/phpunit/data/uploads/test.tsv

     
     1sample  tsv     file    1
     2some    text    here    2
     3yup     it's    text    3
     4sample  tsv     file    1
     5some    text    here    2
     6yup     it's    text    3
     7 No newline at end of file
  • tests/phpunit/data/uploads/test.vtt

     
     1WEBVTT
     2
     300:11.000 --> 00:13.000
     4<v Roger Bingham>We are in New York City
     5
     600:13.000 --> 00:16.000
     7<v Roger Bingham>We’re actually at the Lucern Hotel, just down the street
     8
     900:16.000 --> 00:18.000
     10<v Roger Bingham>from the American Museum of Natural History
     11
     1200:18.000 --> 00:20.000
     13<v Roger Bingham>And with me is Neil deGrasse Tyson
     14
     1500:20.000 --> 00:22.000
     16<v Roger Bingham>Astrophysicist, Director of the Hayden Planetarium
     17
     1800:22.000 --> 00:24.000
     19<v Roger Bingham>at the AMNH.
     20
     2100:24.000 --> 00:26.000
     22<v Roger Bingham>Thank you for walking down here.
     23
     2400:27.000 --> 00:30.000
     25<v Roger Bingham>And I want to do a follow-up on the last conversation we did.
     26
     2700:30.000 --> 00:31.500 align:right size:50%
     28<v Roger Bingham>When we e-mailed—
     29
     3000:30.500 --> 00:32.500 align:left size:50%
     31<v Neil deGrasse Tyson>Didn’t we talk about enough in that conversation?
     32
     3300:32.000 --> 00:35.500 align:right size:50%
     34<v Roger Bingham>No! No no no no; 'cos 'cos obviously 'cos
     35
     3600:32.500 --> 00:33.500 align:left size:50%
     37<v Neil deGrasse Tyson><i>Laughs</i>
     38
     3900:35.500 --> 00:38.000
     40<v Roger Bingham>You know I’m so excited my glasses are falling off here.
     41WEBVTT
     42
     4300:11.000 --> 00:13.000
     44<v Roger Bingham>We are in New York City
     45
     4600:13.000 --> 00:16.000
     47<v Roger Bingham>We’re actually at the Lucern Hotel, just down the street
     48
     4900:16.000 --> 00:18.000
     50<v Roger Bingham>from the American Museum of Natural History
     51
     5200:18.000 --> 00:20.000
     53<v Roger Bingham>And with me is Neil deGrasse Tyson
     54
     5500:20.000 --> 00:22.000
     56<v Roger Bingham>Astrophysicist, Director of the Hayden Planetarium
     57
     5800:22.000 --> 00:24.000
     59<v Roger Bingham>at the AMNH.
     60
     6100:24.000 --> 00:26.000
     62<v Roger Bingham>Thank you for walking down here.
     63
     6400:27.000 --> 00:30.000
     65<v Roger Bingham>And I want to do a follow-up on the last conversation we did.
     66
     6700:30.000 --> 00:31.500 align:right size:50%
     68<v Roger Bingham>When we e-mailed—
     69
     7000:30.500 --> 00:32.500 align:left size:50%
     71<v Neil deGrasse Tyson>Didn’t we talk about enough in that conversation?
     72
     7300:32.000 --> 00:35.500 align:right size:50%
     74<v Roger Bingham>No! No no no no; 'cos 'cos obviously 'cos
     75
     7600:32.500 --> 00:33.500 align:left size:50%
     77<v Neil deGrasse Tyson><i>Laughs</i>
     78
     7900:35.500 --> 00:38.000
     80<v Roger Bingham>You know I’m so excited my glasses are falling off here.
     81 No newline at end of file
  • tests/phpunit/tests/functions.php

     
    10721072        }
    10731073
    10741074        /**
    1075          * Data profider for test_wp_get_image_mime();
     1075         * Data provider for test_wp_get_image_mime();
    10761076         */
    10771077        public function _wp_get_image_mime() {
    10781078                $data = array(
     
    11781178                                        'proper_filename' => false,
    11791179                                ),
    11801180                        ),
     1181                        // Non-image file not allowed even if it's named like one.
     1182                        array(
     1183                                DIR_TESTDATA . '/export/crazy-cdata.xml',
     1184                                'crazy-cdata.jpg',
     1185                                array(
     1186                                        'ext' => false,
     1187                                        'type' => false,
     1188                                        'proper_filename' => false,
     1189                                ),
     1190                        ),
     1191                        // Non-image file not allowed if it's named like something else.
     1192                        array(
     1193                                DIR_TESTDATA . '/export/crazy-cdata.xml',
     1194                                'crazy-cdata.doc',
     1195                                array(
     1196                                        'ext' => false,
     1197                                        'type' => false,
     1198                                        'proper_filename' => false,
     1199                                ),
     1200                        ),
     1201                        // Assorted text/* sample files
     1202                        array(
     1203                                DIR_TESTDATA . '/uploads/test.csv',
     1204                                'test.csv',
     1205                                array(
     1206                                        'ext' => 'csv',
     1207                                        'type' => 'text/csv',
     1208                                        'proper_filename' => false,
     1209                                ),
     1210                        ),
    11811211                );
    11821212
    11831213                // Test a few additional file types on single sites.
     
    12201250        }
    12211251
    12221252        /**
     1253         * @ticket 45615
     1254         * @dataProvider _wp_check_filetype_and_ext_unsupported
     1255         */
     1256        function test_wp_check_filetype_and_ext_unsupported_unfiltered( $file, $filename, $expected ) {
     1257
     1258                // Without a filter adding these file types to the allowed mime_types list, they should all be rejected.
     1259
     1260                $expected_failure = array(
     1261                                        'ext' => false,
     1262                                        'type' => false,
     1263                                        'proper_filename' => false,
     1264                                );
     1265
     1266
     1267                $this->assertEquals( $expected_failure, wp_check_filetype_and_ext( $file, $filename ) );
     1268        }
     1269
     1270        /**
     1271         * @ticket 45615
     1272         * @dataProvider _wp_check_filetype_and_ext_unsupported
     1273         */
     1274        function test_wp_check_filetype_and_ext_unsupported_filtered( $file, $filename, $expected ) {
     1275
     1276                // With a filter these should succeed
     1277                $ext = $expected['ext'];
     1278                $mime_type = $expected['type'];
     1279                $filter = function( $mimes ) use ( $ext, $mime_type ) {
     1280                        $mimes[ $ext ] = $mime_type;
     1281                        return $mimes;
     1282                };
     1283
     1284                add_filter( 'upload_mimes', $filter );
     1285
     1286                $this->assertEquals( $expected, wp_check_filetype_and_ext( $file, $filename ) );
     1287
     1288                remove_filter( 'upload_mimes', $filter );
     1289        }
     1290
     1291        public function _wp_check_filetype_and_ext_unsupported() {
     1292                $data = array(
     1293                        array(
     1294                                        DIR_TESTDATA . '/uploads/test.vtt',
     1295                                        'test.vtt',
     1296                                        array(
     1297                                                        'ext' => 'vtt',
     1298                                                        'type' => 'text/vtt',
     1299                                                        'proper_filename' => false,
     1300                                        ),
     1301                        ),
     1302                        array(
     1303                                        DIR_TESTDATA . '/uploads/test.dfxp',
     1304                                        'test.dfxp',
     1305                                        array(
     1306                                                        'ext' => 'dfxp',
     1307                                                        'type' => 'text/dfxp',
     1308                                                        'proper_filename' => false,
     1309                                        ),
     1310                        ),
     1311                        array(
     1312                                        DIR_TESTDATA . '/uploads/test.json',
     1313                                        'test.json',
     1314                                        array(
     1315                                                        'ext' => 'json',
     1316                                                        'type' => 'application/json',
     1317                                                        'proper_filename' => false,
     1318                                        ),
     1319                        ),
     1320                );
     1321
     1322                return $data;
     1323        }
     1324
     1325        /**
    12231326         * Test file path validation
    12241327         *
    12251328         * @ticket 42016