Test_Import

Test importing recipies functionality.

Contents

  • Methods

  • Source Source

    File: tests/test-import.php

    class Test_Import extends WP_UnitTestCase {
    	private $recipDB;
    
    	public static function wpSetUpBeforeClass( $factory ) {
    
    		// This code will run once before all tests
    
    		$user_id = $factory->user->create( array( 'role' => 'administrator' ) );
    		$user = wp_set_current_user( $user_id );
    
    		// Fake admin visit
    		set_current_screen( 'edit-post' );
    		
    	}
    
    	public function setUp() {
    		parent::setUp();
    		$this->importer = new Import_Recipe;
    		// This code will run once before each test!
    		
    	}
    	 
    	public function tearDown() {
    		parent::tearDown();
    		// This code will run after each test
    	}
    
    	/**
    	 * Test is we are an admin
    	 */
    	public function test_admin() {
    		$this->assertTrue( is_admin() );
    	}
    
    	
    	/**
    	 * Test if automator admin scripts were loaded
    	 */
    	public function test_automator_after_init() {
    
    		$this->assertTrue( did_action( 'automator_after_init' ) > 0 );
    	}
    
    	/**
    	 * Test the class initialization
    	 */
    	public function test_constructor() {
    		// Replace this with some actual testing code.
    		$this->assertTrue( method_exists ( $this->importer->recipe_api , 'add' ) );
    	}
    
    	public function test_load_file() {
    
    		$path = dirname( __FILE__ ) . '/recipes/sample-json.json';
    
    		$expected_result = array (
    			'name' => 'John',
    			'age' => 31,
    			'city' => 'New York',
    			'children' => array("Jack", "Sarah")
    		);
    
    		$actual_result = $this->importer->load_file( $path );
    
    		$this->assertSame( $expected_result, $actual_result );
    	}
    
    	public function test_import_recipe() {
    		$path = dirname( __FILE__ ) . '/recipes/2-triggers-2-actions.json';
    		$recipe_id = $this->importer->import_from_file( $path );
    		//$recipe_id = $this->importer->import_from_array( $recipe );
    
    		// Check that the recipe was added
    		$this->assertSame( $recipe_id, post_exists( "Creates a post when any page is viewed" ) );
    
    		$recipes_data = Automator()->get_recipes_data( true, $recipe_id );
    
    		$recipe_data = array_shift( $recipes_data );
    
    		$this->assertSame( $recipe_data['post_status'], 'publish' );
    
    		$this->assertSame( $recipe_data['triggers'][0]['meta']['code'], 'VIEWPAGE' );
    		$this->assertSame( $recipe_data['triggers'][0]['post_status'], 'publish' );
    		$this->assertSame( $recipe_data['triggers'][0]['meta']['WPPAGE'], '-1' );
    
    		$this->assertSame( $recipe_data['triggers'][1]['meta']['code'], 'VIEWPOST' );
    		$this->assertSame( $recipe_data['triggers'][1]['post_status'], 'publish' );
    		$this->assertSame( $recipe_data['triggers'][1]['meta']['WPPOST'], '-1' );
    
    		$this->assertSame( $recipe_data['actions'][0]['meta']['code'], 'CREATEPOST' );
    		$this->assertSame( $recipe_data['actions'][0]['post_status'], 'publish' );
    		$this->assertSame( $recipe_data['actions'][0]['meta']['WPCPOSTAUTHOR'], '{{admin_email}}' );
    		$this->assertSame( $recipe_data['actions'][0]['meta']['WPCPOSTTITLE'], 'A page was visited at {{current_date}} {{current_time}}' );
    		$this->assertSame( $recipe_data['actions'][0]['meta']['WPCPOSTCONTENT'], 'Test content here {{' . $recipe_data['triggers'][0]['ID'] . ':VIEWPOST:WPPOST_THUMB_URL}}' );
    		$this->assertSame( $recipe_data['actions'][0]['meta']['CPMETA_PAIRS'], '[{"KEY":"xx_custom_key","VALUE":"xx_custom_value"},{"KEY":"zz_custom_key","VALUE":"zz_custom_value"}]' );
    
    
    		$this->assertSame( $recipe_data['actions'][1]['meta']['code'], 'SENDEMAIL' );
    		$this->assertSame( $recipe_data['actions'][1]['post_status'], 'publish' );
    		$this->assertSame( $recipe_data['actions'][1]['meta']['EMAILFROM'], '{{admin_email}}' );
    		$this->assertSame( $recipe_data['actions'][1]['meta']['EMAILFROMNAME'], 'PHPUnit' );
    		$this->assertSame( $recipe_data['actions'][1]['meta']['EMAILTO'], '{{admin_email}}' );
    
    	}
    
    	public function test_add_trigger_exception() {
    
    		$recipe_id = -2;
    		$trigger = array (
    			'code' => 'VIEWPAGE',
    			'status' => 'publish',
    			'fields' => 
    			array (
    			  array (
    				'meta' => 'WPPAGE',
    				'value' => -1,
    			  ),
    			),
    		);
    		$this->expectException(\Exception::class);
    		$recipe_id = $this->importer->add_trigger( $recipe_id, $trigger );
    	}
    
    	public function test_add_action_exception() {
    
    		$recipe_id = -2;
    		$action = array (
    			'code' => 'SENDEMAIL',
    			'status' => 'publish',
    			'fields' => 
    			array (
    			  array (
    				'meta' => 'EMAILFROM',
    				'value' => '{{admin_email}}',
    			  ),
    			),
    		);
    		$this->expectException(\Exception::class);
    		$recipe_id = $this->importer->add_action( $recipe_id, $action );
    	}
    
    	public function test_set_status_exception() {
    
    		$item['ID'] = -1;
    		$item['status'] = 'non-valid-status';
    
    		$this->expectException(\Exception::class);
    
    		$recipe_id = $this->importer->set_status( $item );
    	}
    
    	public function test_set_values_exceptions() {
    
    		$item['ID'] = -1;
    
    		$this->expectException(\Exception::class);
    
    		$recipe_id = $this->importer->set_values( $item );
    
    		$item['fields'] = array (
    			0 => 
    			array (
    			  'meta' => 'EMAILFROM',
    			  'value' => '{{admin_email}}',
    			),
    			1 => 
    			array (
    			  'meta' => 'EMAILFROMNAME',
    			  'value' => 'PHPUnit',
    			),
    			2 => 
    			array (
    			  'meta' => 'EMAILTO',
    			  'value' => '{{admin_email}}',
    			),
    		);
    
    		$this->expectException(\Exception::class);
    
    		$recipe_id = $this->importer->set_values( $item );
    
    	}
    
    }
    

    Methods Methods