Usage_Reports_Tests

Contents

  • Methods

  • Source Source

    File: tests/wpunit/Usage_Reports_Test.php

    class Usage_Reports_Tests extends \Codeception\TestCase\WPTestCase {
    
    	/**
    	 * @var \WP_REST_Server
    	 */
    	protected $server;
    
    	public static $get_blog_count_exists;
    
    	/**
    	 * @var \Usage_Reports
    	 */
    	protected $usage_reports;
    
    	protected $namespaced_route = '/uap/v2/async_report';
    
    	public function setUp(): void {
    		// Before...
    		parent::setUp();
    
    		$this->usage_reports = new Usage_Reports();
    		/** @var WP_REST_Server $wp_rest_server */
    		global $wp_rest_server;
    		$this->server = $wp_rest_server = new \WP_REST_Server();
    		do_action( 'rest_api_init' );
    
    	}
    
    	public function tearDown(): void {
    		// Your tear down methods here.
    
    		// Then...
    		parent::tearDown();
    	}
    
    	public function test_construct() {
    
    		$this->usage_reports = new Usage_Reports();
    
    		//Check that the object is avaiable
    		$this->assertTrue( $this->usage_reports instanceof Usage_Reports );
    
    		// Check that the rest api endpoint exist
    		$routes = $this->server->get_routes( 'uap/v2' );
    			$this->assertArrayHasKey( $this->namespaced_route, $routes );
    
    		// Check that shutdown action is attached
    		$has_action = has_action( 'shutdown', array( $this->usage_reports, 'maybe_report' ) );
    			$this->assertTrue( $has_action == 10 );
    	}
    
    	 /**
    	 * @dataProvider maybe_report_provider
    	 */
    	public function test_maybe_report( $a, $b, $c ) {
    
    		// Create a stub for the Usage_Reports class.
    		$mock = $this->getMockBuilder( Usage_Reports::class )
    			->onlyMethods( array( 'reporting_enabled', 'time_to_report', 'async_report' ) )
    			->getMock();
    
    		$mock->method( 'async_report' )
    			->willReturn( 'Hello World!' );
    
    		// Configure the mock.
    		$mock->method( 'reporting_enabled' )
    			 ->willReturn( $a );
    
    		$mock->method( 'time_to_report' )
    			 ->willReturn( $b );
    
    		$this->assertSame( $c, $mock->maybe_report() );
    
    	}
    
    	/**
    	 * Provides different scensarios for test_maybe_report
    	 */
    	public function maybe_report_provider() {
    		return array(
    			array( false, false, false ),
    			array( false, true, false ),
    			array( true, false, false ),
    			array( true, true, 'Hello World!' ),
    		);
    	}
    
    	public function test_reporting_enabled() {
    
    		$reporting_enabled = $this->usage_reports->reporting_enabled();
    		$this->assertNotTrue( $reporting_enabled );
    
    		update_option( 'automator_reporting', true );
    
    		$reporting_enabled = $this->usage_reports->reporting_enabled();
    		$this->assertTrue( $reporting_enabled );
    
    	}
    
    	public function test_reporting_enabled_when_pro() {
    
    		//Fake a pro installation
    		add_filter(
    			'active_plugins',
    			function( $active_plugins ) {
    				array_push( $active_plugins, 'uncanny-automator-pro/uncanny-automator-pro.php' );
    				return $active_plugins;
    			},
    			10,
    			1
    		);
    
    		// Make sure that we successfully faked Pro
    		$this->assertTrue( is_automator_pro_active() );
    
    		$reporting_enabled = $this->usage_reports->reporting_enabled();
    
    		$this->assertTrue( $reporting_enabled );
    
    		// Now we will disable reporting completely even on Pro
    		define( 'AUTOMATOR_REPORTING', false );
    
    		$reporting_enabled = $this->usage_reports->reporting_enabled();
    		$this->assertNotTrue( $reporting_enabled );
    
    	}
    
    	public function test_time_to_report() {
    
    		delete_option( 'automator_next_report' );
    
    		// If there is no option stored, it's time to report
    		$time_to_report = $this->usage_reports->time_to_report();
    		$this->assertTrue( $time_to_report );
    
    		// If the next report timestamp is in the future, no need to report yet
    		update_option( 'automator_next_report', time() + 1000 );
    
    		$time_to_report = $this->usage_reports->time_to_report();
    		$this->assertNotTrue( $time_to_report );
    
    		// If the next report timestamp is in the past, time to report
    		update_option( 'automator_next_report', time() - 1000 );
    
    		$time_to_report = $this->usage_reports->time_to_report();
    		$this->assertTrue( $time_to_report );
    
    	}
    
    	public function test_validate_rest_call() {
    
    		$request = new \WP_REST_Request( 'POST', 'uap/v2/async_report/' );
    
    		$valid = $this->usage_reports->validate_rest_call( $request );
    
    		$this->assertNotTrue( $valid );
    
    		$next_report = time() + $this->usage_reports->retry_interval;
    
    		update_option( 'automator_next_report', $next_report );
    
    		$request->set_body( 'next_report=' . $next_report );
    
    		$valid = $this->usage_reports->validate_rest_call( $request );
    
    		$this->assertTrue( $valid );
    
    	}
    
    	public function test_async_report() {
    
    		$this->last_request = array();
    
    		// Prevent WordPress from sending actual requests
    		add_filter(
    			'pre_http_request',
    			function( $preempt, $parsed_args, $url ) {
    				$this->last_request = array(
    					'parsed_args' => $parsed_args,
    					'url'         => $url,
    				);
    				return new \WP_Error();
    			},
    			10,
    			3
    		);
    
    		$url = $this->usage_reports->async_report();
    
    		// Check that now there is a report scheduled
    		$this->assertSame( $this->last_request['url'], get_rest_url() . 'uap/v2/async_report/' );
    		$this->assertNotEmpty( $this->last_request['parsed_args']['body']['next_report'] );
    	}
    
    	public function test_call_api() {
    
    		// Prevent WordPress from sending actual requests
    		add_filter(
    			'pre_http_request',
    			function( $preempt, $parsed_args, $url ) {
    				$this->last_request = array(
    					'parsed_args' => $parsed_args,
    					'url'         => $url,
    				);
    				return new \WP_Error();
    			},
    			10,
    			3
    		);
    
    		// Check that no report is scheduled
    		$this->assertNotTrue( get_option( 'automator_next_report' ) );
    
    		$this->usage_reports->call_api();
    
    		$this->assertSame( $this->last_request['url'], AUTOMATOR_API_URL . 'v2/report' );
    		$this->assertTrue( $this->last_request['parsed_args']['body']['action'] === 'save' );
    
    	}
    
    	public function test_sites_count() {
    		self::$get_blog_count_exists = false;
    		$sites_count                 = $this->usage_reports->sites_count();
    		$this->assertSame( 'Not set', $sites_count );
    
    		self::$get_blog_count_exists = true;
    		$sites_count                 = $this->usage_reports->sites_count();
    		$this->assertSame( 42, $sites_count );
    	}
    
    	public function test_get_data() {
    		$data = $this->usage_reports->get_data();
    
    		$this->assertArrayHasKey( 'server', $data );
    		$this->assertArrayHasKey( 'wp', $data );
    		$this->assertArrayHasKey( 'automator', $data );
    		$this->assertArrayHasKey( 'license', $data );
    		$this->assertArrayHasKey( 'active_plugins', $data );
    		$this->assertArrayHasKey( 'theme', $data );
    		$this->assertArrayHasKey( 'recipes', $data );
    		$this->assertArrayHasKey( 'integrations', $data );
    		$this->assertArrayHasKey( 'get_data_took', $data );
    
    	}
    
    	public function test_get_plugins_info() {
    
    		$Automator_System_Report = Automator_System_Report::get_instance();
    
    		$system_report = $Automator_System_Report->get();
    
    		$active_plugins = $system_report['active_plugins'];
    
    		$active_plugins[] = array(
    			'name'    => ' ',
    			'version' => '',
    		);
    
    		$active_plugins[] = array(
    			'name'    => ' ',
    			'version' => '',
    		);
    
    		$active_plugins[] = array(
    			'name'    => 'Some plugin',
    			'version' => '2.4',
    		);
    
    		$plugins_info = $this->usage_reports->get_plugins_info( $active_plugins );
    
    		$this->assertTrue( 2 === count( $plugins_info ) );
    	}
    
    	public function test_get_automator_info() {
    		$data = $this->usage_reports->get_data();
    		$this->assertFalse( isset( $data['automator']['pro_version'] ) );
    
    		// Now we will immitade that Pro is enabled
    		define( 'AUTOMATOR_PRO_PLUGIN_VERSION', '77' );
    
    		$data = $this->usage_reports->get_data();
    		$this->assertSame( '77', $data['automator']['pro_version'] );
    
    	}
    
    	public function test_schedule_next_report() {
    
    		// If the API has responded with the timestamp
    		$body = array( 'data' => array( 'report_frequency' => WEEK_IN_SECONDS ) );
    
    		$this->usage_reports->schedule_next_report( $body );
    
    		$next_report = get_option( 'automator_next_report' );
    
    		// Check if the report is scheduled within 6-8 days
    		$is_next_day = $next_report > time() + DAY_IN_SECONDS * 6 && $next_report < time() + DAY_IN_SECONDS * 8;
    
    		$this->assertTrue( $is_next_day );
    
    	}
    
    	public function test_get_recipes_info() {
    
    		// There should be recipes by default
    		$this->assertNull( $this->usage_reports->get_recipes_info() );
    
    		// Add some recipe data
    		$this->usage_reports->recipes_data = $this->dummy_recipes();
    
    		$this->usage_reports->get_recipes_info();
    
    		$this->assertTrue( $this->usage_reports->report['recipes']['total_integrations_used'] > 0 );
    
    	}
    
    	public function test_process_recipe_data() {
    
    		$live_recipes_count_before     = $this->usage_reports->report['recipes']['live_recipes_count'];
    		$user_recipes_count_before     = $this->usage_reports->report['recipes']['user_recipes_count'];
    		$everyone_recipes_count_before = $this->usage_reports->report['recipes']['everyone_recipes_count'];
    
    		$recipe_data = array(
    			'post_status' => 'publish',
    			'recipe_type' => 'user',
    		);
    
    		$this->usage_reports->process_recipe_data( $recipe_data );
    
    		$recipe_data = array(
    			'post_status' => 'publish',
    			'recipe_type' => 'anonymous',
    		);
    
    		$this->usage_reports->process_recipe_data( $recipe_data );
    
    		$live_recipes_count_after     = $this->usage_reports->report['recipes']['live_recipes_count'];
    		$user_recipes_count_after     = $this->usage_reports->report['recipes']['user_recipes_count'];
    		$everyone_recipes_count_after = $this->usage_reports->report['recipes']['everyone_recipes_count'];
    
    		$this->assertTrue( $live_recipes_count_after - $live_recipes_count_before == 2 );
    		$this->assertTrue( $user_recipes_count_after - $user_recipes_count_before == 1 );
    		$this->assertTrue( $everyone_recipes_count_after - $everyone_recipes_count_before == 1 );
    	}
    
    	public function test_process_items() {
    
    		$type                 = 'triggers';
    		$recipe_data[ $type ] = null;
    		$this->assertNull( $this->usage_reports->process_items( $type, $recipe_data ) );
    
    	}
    
    	public function test_process_async_actions() {
    		$data = array();
    
    		$this->assertTrue( $this->usage_reports->report['recipes']['delayed_actions_count'] == 0 );
    
    		$data['meta']['async_mode'] = 'delay';
    		$this->usage_reports->process_async_actions( $data );
    
    		$this->assertTrue( $this->usage_reports->report['recipes']['delayed_actions_count'] == 1 );
    
    		$this->assertTrue( $this->usage_reports->report['recipes']['scheduled_actions_count'] == 0 );
    
    		$data['meta']['async_mode'] = 'schedule';
    		$this->usage_reports->process_async_actions( $data );
    
    		$this->assertTrue( $this->usage_reports->report['recipes']['scheduled_actions_count'] == 1 );
    
    		$this->assertTrue( $this->usage_reports->report['recipes']['async_actions_count'] == 2 );
    
    	}
    
    	public function test_count_integration() {
    
    		$meta = array(
    			'integration'      => 'WP',
    			'integration_name' => 'WordPress',
    			'code'             => 'VIEWPAGE',
    		);
    
    		$type = 'triggers';
    
    		$this->usage_reports->count_integration( $meta, $type );
    
    		$meta = array(
    			'integration'      => 'LD',
    			'integration_name' => 'LearnDash',
    			'code'             => 'ENROLL',
    		);
    
    		$type = 'actions';
    
    		$this->usage_reports->count_integration( $meta, $type );
    		$this->usage_reports->count_integration( $meta, $type );
    
    		$this->assertTrue( $this->usage_reports->report['integrations'][ $meta['integration'] ][ $type ][ $meta['code'] ] == 2 );
    
    		$this->assertTrue( 2 === count( $this->usage_reports->report['integrations_array'] ) );
    	}
    
    	public function dummy_recipes() {
    		return array(
    			0 => array(
    				'post_status'               => 'publish',
    				'recipe_type'               => 'user',
    				'triggers'                  =>
    				array(
    					0 =>
    					array(
    						'post_status' => 'publish',
    						'meta'        =>
    						array(
    							'code'             => 'VIEWPAGE',
    							'integration'      => 'WP',
    							'integration_name' => 'WordPress',
    						),
    						'tokens'      =>
    						array(),
    					),
    				),
    				'actions'                   =>
    				array(
    					0 =>
    					array(
    						'post_status' => 'publish',
    						'meta'        =>
    						array(
    							'code'             => 'CREATEPOST',
    							'integration'      => 'WP',
    							'integration_name' => 'WordPress',
    						),
    					),
    					1 =>
    					array(
    						'post_status' => 'draft',
    						'meta'        =>
    						array(
    							'code'             => 'CREATEPOST',
    							'integration'      => 'WP',
    							'integration_name' => 'WordPress',
    						),
    					),
    				),
    				'closures'                  =>
    				array(),
    				'completed_by_current_user' => false,
    			),
    			1 => array(
    				'post_status'               => 'draft',
    				'recipe_type'               => 'user',
    				'triggers'                  =>
    				array(
    					0 =>
    					array(
    						'post_status' => 'publish',
    						'meta'        =>
    						array(
    							'code'             => 'UOAERRORS',
    							'integration'      => 'UOA',
    							'integration_name' => 'Automator Core',
    						),
    					),
    

    Methods Methods