/**
  * Test write operations with overriding locale values
  */
 public function testWriteWithLocale()
 {
     // Test creation in default locale
     $item = new FluentTest_TranslatedObject();
     $item->Title = 'Test Title';
     $item->write();
     $itemID = $item->ID;
     // Test basic detail
     $item = FluentTest_TranslatedObject::get()->byId($itemID);
     $this->assertEquals('Test Title', $item->Title);
     // Test update in alternate locale
     Fluent::with_locale('es_ES', function () use($itemID) {
         $item = FluentTest_TranslatedObject::get()->byId($itemID);
         $item->Title = 'Spanish Title';
         $item->write();
     });
     // Default title is unchanged
     $item = FluentTest_TranslatedObject::get()->byId($itemID);
     $this->assertEquals('Test Title', $item->Title);
     // Test previously set alternate locale title change persists
     $esTitle = Fluent::with_locale('es_ES', function () use($itemID) {
         $item = FluentTest_TranslatedObject::get()->byId($itemID);
         return $item->Title;
     });
     $this->assertEquals('Spanish Title', $esTitle);
     // Test object created in alternate locale
     $item2ID = Fluent::with_locale('es_ES', function () {
         $item2 = new FluentTest_TranslatedObject();
         $item2->Title = 'Spanish 2';
         $item2->write();
         return $item2->ID;
     });
     // Default title should be set
     $item2 = FluentTest_TranslatedObject::get()->byId($item2ID);
     $this->assertEquals('Spanish 2', $item2->Title);
     // Change title
     $item2->Title = 'English 2';
     $item2->write();
     // check alternate locale title unchanged
     $es2Title = Fluent::with_locale('es_ES', function () use($item2ID) {
         $item2 = FluentTest_TranslatedObject::get()->byId($item2ID);
         return $item2->Title;
     });
     $this->assertEquals($es2Title, 'Spanish 2');
     // Test that object selected in default locale has the recently changed title
     $item2 = FluentTest_TranslatedObject::get()->byId($item2ID);
     $this->assertEquals('English 2', $item2->Title);
 }
 /**
  * Test that records created in non-default locale don't have missing values for default fields
  */
 public function testCreateInNonDefaultLocale()
 {
     Fluent::set_persist_locale('es_ES');
     // Create a record in this locale
     $record = new FluentTest_TranslatedObject();
     $record->Title = 'es title';
     $record->Description = 'es description';
     $record->write();
     $recordID = $record->ID;
     $row = DB::query(sprintf("SELECT * FROM \"FluentTest_TranslatedObject\" WHERE ID = %d", $recordID))->first();
     // Check that the necessary fields are assigned
     $this->assertEquals('es title', $row['Title']);
     $this->assertEquals('es title', $row['Title_es_ES']);
     $this->assertEquals('es title', $row['Title_fr_CA']);
     $this->assertEmpty($row['Title_en_NZ']);
     $this->assertEquals('es description', $row['Description']);
     $this->assertEquals('es description', $row['Description_es_ES']);
     $this->assertEquals('es description', $row['Description_fr_CA']);
     $this->assertEmpty($row['Description_en_NZ']);
     // modify locale in default locale
     Fluent::with_locale('fr_CA', function () use($recordID) {
         $record = FluentTest_TranslatedObject::get()->byID($recordID);
         $record->Title = 'new ca title';
         $record->write();
     });
     // Check that the necessary fields are assigned
     $row = DB::query(sprintf("SELECT * FROM \"FluentTest_TranslatedObject\" WHERE ID = %d", $recordID))->first();
     $this->assertEquals('new ca title', $row['Title']);
     $this->assertEquals('es title', $row['Title_es_ES']);
     $this->assertEquals('new ca title', $row['Title_fr_CA']);
     $this->assertEmpty($row['Title_en_NZ']);
     $this->assertEquals('es description', $row['Description']);
     $this->assertEquals('es description', $row['Description_es_ES']);
     $this->assertEquals('es description', $row['Description_fr_CA']);
     $this->assertEmpty($row['Description_en_NZ']);
     // modify in another locale
     Fluent::with_locale('en_NZ', function () use($recordID) {
         $record = FluentTest_TranslatedObject::get()->byID($recordID);
         $record->Title = 'nz title';
         $record->Description = 'nz description';
         $record->write();
     });
     // Check that the necessary fields are assigned
     $row = DB::query(sprintf("SELECT * FROM \"FluentTest_TranslatedObject\" WHERE ID = %d", $recordID))->first();
     $this->assertEquals('new ca title', $row['Title']);
     $this->assertEquals('es title', $row['Title_es_ES']);
     $this->assertEquals('new ca title', $row['Title_fr_CA']);
     $this->assertEquals('nz title', $row['Title_en_NZ']);
     $this->assertEquals('es description', $row['Description']);
     $this->assertEquals('es description', $row['Description_es_ES']);
     $this->assertEquals('es description', $row['Description_fr_CA']);
     $this->assertEquals('nz description', $row['Description_en_NZ']);
 }