Make WordPress Core

Ticket #43546: 43546.wpPrivacyDeleteOldExportFiles.diff

File 43546.wpPrivacyDeleteOldExportFiles.diff, 2.9 KB (added by allendav, 7 years ago)

Unit tests for wp_privacy_delete_old_export_files

  • tests/phpunit/tests/privacy/wpPrivacyDeleteOldExportFiles.php

     
     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*/
     18class Tests_Privacy_wpPrivacyDeleteOldExportFiles extends WP_UnitTestCase {
     19        /**
     20         * Silence is golden index.html path
     21         *
     22         * @since 4.9.6
     23         *
     24         * @var string $index_path
     25         */
     26        protected static $index_path;
     27
     28        /**
     29         * Old export file path
     30         *
     31         * @since 4.9.6
     32         *
     33         * @var string $old_export_file_path
     34         */
     35        protected static $old_export_file_path;
     36
     37        /**
     38         * New export file path
     39         *
     40         * @since 4.9.6
     41         *
     42         * @var string $old_export_file_path
     43         */
     44        protected static $new_export_file_path;
     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_normalize_path( trailingslashit( 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::$old_export_file_path = $exports_dir . 'wp-personal-data-file-user-at-example-com-0123456789abcdef.zip';
     60                self::$new_export_file_path = $exports_dir . 'wp-personal-data-file-user-at-example-com-fedcba9876543210.zip';
     61
     62                wp_delete_file( self::$index_path );
     63                wp_delete_file( self::$old_export_file_path );
     64                wp_delete_file( self::$new_export_file_path );
     65        }
     66
     67        public function setUp() {
     68                parent::setUp();
     69                remove_all_filters( 'wp_privacy_exports_dir' );
     70        }
     71
     72        public function test_wp_privacy_delete_old_export_files_ignores_missing_exports_dir() {
     73                add_filter( 'wp_privacy_exports_dir', array( $this, 'filter_bad_exports_dir' ) );
     74                wp_privacy_delete_old_export_files();
     75
     76                // No notice? We pass!
     77                $this->assertTrue( true );
     78        }
     79
     80        function filter_bad_exports_dir( $file ) {
     81                return dirname( __FILE__ ) . 'invalid';
     82        }
     83
     84        public function test_wp_privacy_delete_old_export_files_deletes_just_old_files() {
     85                touch( self::$index_path, time() - 5 * DAY_IN_SECONDS );
     86                touch( self::$old_export_file_path, time() - 5 * DAY_IN_SECONDS );
     87                touch( self::$new_export_file_path, time() );
     88
     89                wp_privacy_delete_old_export_files();
     90
     91                $this->assertTrue( file_exists( self::$index_path ) );
     92                $this->assertFalse( file_exists( self::$old_export_file_path ) );
     93                $this->assertTrue( file_exists( self::$new_export_file_path ) );
     94        }
     95}