/**
  * Tests to see if the versions for a snippet are actually removed when the snippet is deleted using a call to DataObject::delete()
  */
 public function testVersionCleanupOnDelete()
 {
     $snippet = $this->objFromFixture('Snippet', 'snippet1');
     $snippetID = $snippet->ID;
     //Verify that there are snippet versions for the located snippet
     $this->assertGreaterThan(0, SnippetVersion::get()->filter('ParentID', $snippetID)->count(), 'Snippet versions cannot be found');
     //Delete the snippet
     $snippet->delete();
     //Test to see if the versions have been properly removed
     $this->assertEquals(0, SnippetVersion::get()->filter('ParentID', $snippetID)->count(), 'Snippet versions were found');
 }
 /**
  * Gets the snippet text for the two revisions as well as a unified diff file of the revisions
  * @param {stdClass} $data Data passed from ActionScript
  * @return {array} Standard response base
  */
 public function getSnippetDiff($data)
 {
     $response = CodeBank_ClientAPI::responseBase();
     //Get the Main Revision
     $snippet1 = SnippetVersion::get()->byID(intval($data->mainRev));
     if (empty($snippet1) || $snippet1 === false || $snippet1->ID == 0) {
         $response['status'] = 'EROR';
         $response['message'] = _t('CodeBankAPI.MAIN_REVISION_NOT_FOUND', '_Main revision not found');
         return $response;
     }
     $snippet1 = preg_replace('/\\r\\n|\\n|\\r/', "\n", $snippet1->Text);
     //Get the Comparision Revision
     $snippet2 = SnippetVersion::get()->byID(intval($data->compRev));
     if (empty($snippet2) || $snippet1 === false || $snippet2->ID == 0) {
         $response['status'] = 'EROR';
         $response['message'] = _t('CodeBankAPI.COMPARE_REVISION_NOT_FOUND', '_Compare revision not found');
         return $response;
     }
     $snippet2 = preg_replace('/\\r\\n|\\n|\\r/', "\n", $snippet2->Text);
     //Generate the diff file
     $diff = new Text_Diff('auto', array(preg_split('/\\n/', $snippet2), preg_split('/\\n/', $snippet1)));
     $renderer = new Text_Diff_Renderer_unified(array('leading_context_lines' => 1, 'trailing_context_lines' => 1));
     $response['data'] = array('mainRev' => $snippet1, 'compRev' => $snippet2, 'diff' => $renderer->render($diff));
     return $response;
 }
 /**
  * Removes all version history for this snippet before deleting the snippet record
  */
 protected function onBeforeDelete()
 {
     parent::onBeforeDelete();
     SnippetVersion::get()->filter('ParentID', $this->ID)->removeAll();
 }