2011-03-14 04:47:44 +01:00
|
|
|
<?php
|
2016-07-04 02:44:09 +02:00
|
|
|
|
2017-11-16 22:58:14 +01:00
|
|
|
namespace SilverStripe\PostgreSQL\Tests;
|
|
|
|
|
|
|
|
use Exception;
|
|
|
|
use Page;
|
2016-09-01 06:22:17 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2016-07-04 02:44:09 +02:00
|
|
|
use SilverStripe\ORM\DataObject;
|
2016-09-01 06:22:17 +02:00
|
|
|
use SilverStripe\ORM\DB;
|
2016-07-04 02:44:09 +02:00
|
|
|
use SilverStripe\PostgreSQL\PostgreSQLDatabase;
|
|
|
|
|
2011-03-14 04:47:44 +01:00
|
|
|
/**
|
|
|
|
* @package postgresql
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
2015-12-17 19:18:01 +01:00
|
|
|
class PostgreSQLDatabaseTest extends SapphireTest
|
|
|
|
{
|
2017-07-26 15:24:11 +02:00
|
|
|
protected $usesDatabase = true;
|
|
|
|
|
2015-12-17 19:18:01 +01:00
|
|
|
public function testReadOnlyTransaction()
|
|
|
|
{
|
2017-11-16 22:58:14 +01:00
|
|
|
if (DB::get_conn()->supportsTransactions() == true
|
2015-12-17 19:18:01 +01:00
|
|
|
&& DB::get_conn() instanceof PostgreSQLDatabase
|
|
|
|
) {
|
2017-11-16 22:58:14 +01:00
|
|
|
$page = new Page();
|
|
|
|
$page->Title = 'Read only success';
|
2015-12-17 19:18:01 +01:00
|
|
|
$page->write();
|
|
|
|
|
|
|
|
DB::get_conn()->transactionStart('READ ONLY');
|
|
|
|
|
|
|
|
try {
|
2017-11-16 22:58:14 +01:00
|
|
|
$page = new Page();
|
|
|
|
$page->Title = 'Read only page failed';
|
2015-12-17 19:18:01 +01:00
|
|
|
$page->write();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
//could not write this record
|
|
|
|
//We need to do a rollback or a commit otherwise we'll get error messages
|
|
|
|
DB::get_conn()->transactionRollback();
|
|
|
|
}
|
|
|
|
|
|
|
|
DB::get_conn()->transactionEnd();
|
|
|
|
|
|
|
|
DataObject::flush_and_destroy_cache();
|
|
|
|
|
2017-11-16 22:58:14 +01:00
|
|
|
$success = DataObject::get('Page', "\"Title\"='Read only success'");
|
|
|
|
$fail = DataObject::get('Page', "\"Title\"='Read only page failed'");
|
2015-12-17 19:18:01 +01:00
|
|
|
|
|
|
|
//This page should be in the system
|
|
|
|
$this->assertTrue(is_object($success) && $success->exists());
|
|
|
|
|
|
|
|
//This page should NOT exist, we had 'read only' permissions
|
|
|
|
$this->assertFalse(is_object($fail) && $fail->exists());
|
|
|
|
} else {
|
|
|
|
$this->markTestSkipped('Current database is not PostgreSQL');
|
|
|
|
}
|
|
|
|
}
|
2013-04-03 06:27:11 +02:00
|
|
|
}
|