2017-12-04 13:25:35 +00:00
|
|
|
<?php
|
2019-06-28 14:24:59 -04:00
|
|
|
|
2017-12-04 13:25:35 +00:00
|
|
|
namespace Tests\Feature;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
use ProcessMaker\Model\DbSource;
|
|
|
|
|
use ProcessMaker\Model\Process;
|
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
|
|
class DBQueryTest extends TestCase
|
|
|
|
|
{
|
2019-06-28 14:24:59 -04:00
|
|
|
|
2017-12-04 13:25:35 +00:00
|
|
|
use DatabaseTransactions;
|
|
|
|
|
|
|
|
|
|
/**
|
2019-06-28 14:24:59 -04:00
|
|
|
* Sets up the unit tests.
|
|
|
|
|
*/
|
|
|
|
|
protected function setUp()
|
|
|
|
|
{
|
2019-08-08 09:51:39 -04:00
|
|
|
parent::setUp();
|
2019-06-28 14:24:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Verify the execution of a common SQL statement.
|
2020-06-10 23:00:46 +00:00
|
|
|
* Note, this test is now using Laravel DB backend work
|
2019-06-28 14:24:59 -04:00
|
|
|
* @test
|
2017-12-04 13:25:35 +00:00
|
|
|
*/
|
|
|
|
|
public function testStandardExecuteQuery()
|
|
|
|
|
{
|
|
|
|
|
$results = executeQuery("SELECT * FROM USERS WHERE USR_UID = '00000000000000000000000000000001'");
|
|
|
|
|
$this->assertCount(1, $results);
|
|
|
|
|
// Note, we check index 1 because results from executeQuery are 1 indexed, not 0 indexed.
|
2019-06-28 14:24:59 -04:00
|
|
|
$expected = [
|
2017-12-04 13:25:35 +00:00
|
|
|
'USR_UID' => '00000000000000000000000000000001',
|
|
|
|
|
'USR_USERNAME' => 'admin'
|
2019-06-28 14:24:59 -04:00
|
|
|
];
|
|
|
|
|
$this->assertArraySubset($expected, $results[1]);
|
2017-12-04 13:25:35 +00:00
|
|
|
}
|
|
|
|
|
|
2019-06-28 14:24:59 -04:00
|
|
|
/**
|
|
|
|
|
* Verify the existence of the admin user.
|
|
|
|
|
* @test
|
|
|
|
|
*/
|
2017-12-04 13:25:35 +00:00
|
|
|
public function testDBFacadeQuery()
|
|
|
|
|
{
|
2019-06-28 14:24:59 -04:00
|
|
|
$record = DB::table('USERS')->where('USR_UID', '=', '00000000000000000000000000000001')->first();
|
2017-12-04 13:25:35 +00:00
|
|
|
$this->assertEquals('admin', $record->USR_USERNAME);
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-28 14:24:59 -04:00
|
|
|
/**
|
|
|
|
|
* Verify the execution of a SQL statement common to an MySQL external database.
|
|
|
|
|
* @test
|
|
|
|
|
*/
|
2017-12-04 13:25:35 +00:00
|
|
|
public function testStandardExecuteQueryWithExternalMySQLDatabase()
|
|
|
|
|
{
|
|
|
|
|
// Our test external database is created in our tests/bootstrap.php file
|
|
|
|
|
// We'll use our factories to create our process and database
|
|
|
|
|
$process = factory(Process::class)->create();
|
|
|
|
|
// Let's create an external DB to ourselves
|
|
|
|
|
$externalDB = factory(DbSource::class)->create([
|
2019-06-28 14:24:59 -04:00
|
|
|
'DBS_SERVER' => config('database.connections.testexternal.host'),
|
2017-12-04 13:25:35 +00:00
|
|
|
'DBS_PORT' => '3306',
|
2019-06-28 14:24:59 -04:00
|
|
|
'DBS_USERNAME' => config('database.connections.testexternal.username'),
|
2017-12-04 13:25:35 +00:00
|
|
|
// Remember, we have to do some encryption here @see DbSourceFactory.php
|
2021-02-04 18:07:47 -04:00
|
|
|
'DBS_PASSWORD' => \G::encrypt(env('DB_PASSWORD'), config('database.connections.testexternal.database'), false, false) . "_2NnV3ujj3w",
|
2019-06-28 14:24:59 -04:00
|
|
|
'DBS_DATABASE_NAME' => config('database.connections.testexternal.database'),
|
2017-12-04 13:25:35 +00:00
|
|
|
'PRO_UID' => $process->PRO_UID
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
// Now set our process ID
|
|
|
|
|
/**
|
|
|
|
|
* @todo Migrate to non session based process store
|
|
|
|
|
*/
|
|
|
|
|
$_SESSION['PROCESS'] = $process->PRO_UID;
|
|
|
|
|
// Perform test
|
|
|
|
|
$results = executeQuery('SELECT * FROM test', $externalDB->DBS_UID);
|
|
|
|
|
$this->assertCount(1, $results);
|
|
|
|
|
$this->assertEquals('testvalue', $results[1]['value']);
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-28 14:24:59 -04:00
|
|
|
/**
|
|
|
|
|
* Verify the execution of a SQL statement common to an MSSQL external database.
|
|
|
|
|
* @test
|
|
|
|
|
*/
|
2017-12-04 13:25:35 +00:00
|
|
|
public function testStandardExecuteQueryWithExternalMSSqlDatabase()
|
|
|
|
|
{
|
2019-06-28 14:24:59 -04:00
|
|
|
if (!env('RUN_MSSQL_TESTS')) {
|
2017-12-04 13:25:35 +00:00
|
|
|
$this->markTestSkipped('MSSQL Related Test Skipped');
|
|
|
|
|
}
|
|
|
|
|
// Our test external database is created in our tests/bootstrap.php file
|
|
|
|
|
// We'll use our factories to create our process and database
|
|
|
|
|
$process = factory(Process::class)->create();
|
|
|
|
|
// Let's create an external DB to ourselves
|
|
|
|
|
$externalDB = factory(DbSource::class)->create([
|
|
|
|
|
'DBS_SERVER' => env('MSSQL_HOST'),
|
|
|
|
|
'DBS_PORT' => env('MSSQL_PORT'),
|
|
|
|
|
'DBS_TYPE' => 'mssql',
|
|
|
|
|
'DBS_USERNAME' => env('MSSQL_USERNAME'),
|
|
|
|
|
// Remember, we have to do some encryption here @see DbSourceFactory.php
|
2021-02-04 18:07:47 -04:00
|
|
|
'DBS_PASSWORD' => \G::encrypt(env('MSSQL_PASSWORD'), env('MSSQL_DATABASE'), false, false) . "_2NnV3ujj3w",
|
2019-06-28 14:24:59 -04:00
|
|
|
'DBS_DATABASE_NAME' => env('MSSQL_DATABASE'),
|
2017-12-04 13:25:35 +00:00
|
|
|
'PRO_UID' => $process->PRO_UID
|
|
|
|
|
]);
|
|
|
|
|
// Now set our process ID
|
|
|
|
|
/**
|
|
|
|
|
* @todo Migrate to non session based process store
|
|
|
|
|
*/
|
|
|
|
|
$_SESSION['PROCESS'] = $process->PRO_UID;
|
|
|
|
|
// Perform test
|
|
|
|
|
$results = executeQuery('SELECT * FROM test', $externalDB->DBS_UID);
|
|
|
|
|
$this->assertCount(1, $results);
|
|
|
|
|
$this->assertEquals('testvalue', $results[1]['value']);
|
|
|
|
|
}
|
2019-06-28 14:24:59 -04:00
|
|
|
}
|