Skip to content
Snippets Groups Projects
EntryQueriesTest.php 2.16 KiB
Newer Older
  • Learn to ignore specific revisions
  • Josh Pollock's avatar
    Josh Pollock committed
    <?php
    
    
    namespace calderawp\CalderaFormsQuery\Tests\Integration;
    
    
    
    Josh Pollock's avatar
    Josh Pollock committed
    use calderawp\CalderaFormsQuery\SelectsCreatesSelect;
    
    use calderawp\CalderaFormsQuery\Tests\Traits\CanCreateEntryWithEmailField;
    
    Josh Pollock's avatar
    Josh Pollock committed
    use calderawp\CalderaFormsQuery\Tests\Traits\HasFactories;
    use calderawp\CalderaFormsQuery\Tests\Traits\UsersMockFormAsDBForm;
    
    class EntryQueriesTest extends IntegrationTestCase
    {
    	use UsersMockFormAsDBForm;
    	public function setUp()
    	{
    		global $wpdb;
    		$tables = new \Caldera_Forms_DB_Tables($wpdb);
    		$tables->add_if_needed();
    		$this->set_mock_form();
    		$this->mock_form_id = \Caldera_Forms_Forms::import_form( $this->mock_form );
    		$this->mock_form = \Caldera_Forms_Forms::get_form( $this->mock_form_id );
    		parent::setUp();
    	}
    
    	/**
    	 * Test that getResults method runs queries against WordPress correctly
    	 *
    
    Josh Pollock's avatar
    Josh Pollock committed
    	 * @covers SelectsCreatesSelect::getResults()
    
    Josh Pollock's avatar
    Josh Pollock committed
    	 */
    	public function testGetResultsCanDoSQL(){
    		$details = $this->create_entry( $this->mock_form );
    		$details = $this->create_entry( $this->mock_form );
    		global $wpdb;
    		$sql = $wpdb->prepare( "SELECT COUNT(`id`) AS `total` FROM `" . $wpdb->prefix . "cf_form_entries` WHERE `form_id` = %s", $this->mock_form_id );
    		$results = $this->entryQueriesFactory()->getResults( $sql );
    		$this->assertTrue( ! empty( $results ) );
    		$this->assertEquals( 2, $results[0]->total);
    
    		$wpdbDirectResults = $wpdb->get_results( $sql );
    		$this->assertEquals( 2, $wpdbDirectResults[0]->total);
    	}
    
    	/**
    	 * Test that we can run queries and the environment can update DB
    	 *
    
    Josh Pollock's avatar
    Josh Pollock committed
    	 * @covers SelectsCreatesSelect::getResults()
    
    Josh Pollock's avatar
    Josh Pollock committed
    	 */
    	public function testCanQuery()
    	{
    		global $wpdb;
    		$sql = $wpdb->prepare( "SELECT COUNT(`id`) AS `total` FROM `" . $wpdb->prefix . "cf_form_entries` WHERE `form_id` = %s", $this->mock_form_id );
    		$resultsOne = $this->entryQueriesFactory()->getResults( $sql );
    		$this->assertTrue( ! empty( $resultsOne ) );
    
    		$entry_details = $this->create_entry( $this->mock_form );
    		$sql = $wpdb->prepare("SELECT * FROM `" . $wpdb->prefix . "cf_form_entries` WHERE `id` = %s", $entry_details['id'] );
    		$resultsTwo = $this->entryQueriesFactory()->getResults( $sql );
    		$this->assertTrue( ! empty( $resultsTwo ), var_export( $resultsTwo, true ) );
    
    	}