From 6019b710d00a8537fb822034378e325cee26cd3c Mon Sep 17 00:00:00 2001
From: Marco Villegas <marvil07@gmail.com>
Date: Mon, 13 Dec 2010 19:10:31 -0500
Subject: [PATCH] Refactor VersioncontrolRepositoryUnitTestingTestCase to test all CRUD.

- Test each table field for repository Creation test.
- Add tests for Update and Delete.
- Use directly the controller for Read instead of the backend method.
- Verify with direct database queries.
- Assert versioncontrolCreateRepository() method and use it at repo unit
  tests.
---
 tests/VersioncontrolBackendTests.test              |    2 +
 ...ersioncontrolRepositoryUnitTestingTestCase.test |  109 ++++++++++++++++----
 tests/VersioncontrolTestCase.test                  |    6 +-
 3 files changed, 97 insertions(+), 20 deletions(-)

diff --git tests/VersioncontrolBackendTests.test tests/VersioncontrolBackendTests.test
index 20ed6e5..5d4e065 100644
--- tests/VersioncontrolBackendTests.test
+++ tests/VersioncontrolBackendTests.test
@@ -43,3 +43,5 @@ class VersioncontrolBackendBaseUnitTest extends VersioncontrolTestCase {
     }
   }
 }
+
+//TODO test backend methods on another test class with the test backend
diff --git tests/VersioncontrolRepositoryUnitTestingTestCase.test tests/VersioncontrolRepositoryUnitTestingTestCase.test
index d1f222f..10dc4b2 100644
--- tests/VersioncontrolRepositoryUnitTestingTestCase.test
+++ tests/VersioncontrolRepositoryUnitTestingTestCase.test
@@ -16,7 +16,6 @@ class VersioncontrolRepositoryUnitTestingTestCase extends VersioncontrolTestCase
    */
   public static function getInfo() {
     return array(
-      // FIXME
       'name' => t('Repository class unit tests'),
       'description' => t('Unit tests for the VersioncontrolRepository class.'),
       'group' => t('Version Control API'),
@@ -31,30 +30,102 @@ class VersioncontrolRepositoryUnitTestingTestCase extends VersioncontrolTestCase
     parent::setUp();
   }
 
-  public function testBadLoadParams() {
-    $repository = $this->testBackend->loadEntities('repo', array(''));
-    $this->assertTrue(empty($repository), t('Make sure we get an empty array when trying to get a repository with empty repo_id'), 'controller');
-
-    $repository = $this->testBackend->loadEntities('repo', NULL);
-    $this->assertTrue(empty($repository), t('Make sure we get an empty array when trying to get a repository with a NULL repo_id'), 'controller');
-
-    $repository = $this->testBackend->loadEntities('repo', array(1));
-    $this->assertTrue(empty($repository), t('Make sure we get an empty array when trying to get a repository which do not exit'), 'controller');
-  }
-
-  public function testRepositoryLoadParams() {
+  public function testCreation() {
+    // Build and insert.
     $name = $this->randomName();
     $data = array(
       'name' => $name,
-      'vcs' => 'test',
-      'backend' => $this->testBackend,
       'root' => '/fake/path/to/repo',
+      'update_method' => 0,
+      'updated' => 0,
+      'locked' => 0,
+      'data' => array(),
+      'plugins' => array('auth_handler' => 'ffa', 'author_mapper' => 'simple_name', 'committer_mapper' => 'simple_mail'),
     );
     $repo = $this->testBackend->buildEntity('repo', $data);
     $repo->insert();
-    $this->assertTrue(isset($repo->repo_id) && is_numeric($repo->repo_id), t('VersioncontrolRepository::insert() properly populates a new repository object with an integer repo_id.'));
-    $repositories = $this->testBackend->loadEntities('repo', array($repo->repo_id));
-    $this->assertTrue(isset($repositories[$repo->repo_id]) && is_a($repositories[$repo->repo_id], 'VersioncontrolRepository'), t('Make sure we get a valid return when passing a good repo_id.'));
-    $this->assertEqual($repositories[$repo->repo_id]->repo_id, $repo->repo_id, t('Make sure we get the right repository.'), 'controller');
+
+    // Verify.
+    $db_repo = db_select('versioncontrol_repositories', 'r')
+      ->fields('r')
+      ->condition('repo_id', $repo->repo_id)
+      ->execute()
+      ->fetchObject();
+    foreach ($data as $key => $val) {
+      if ($key == 'data' || $key == 'plugins') {
+        $db_repo->$key = unserialize($db_repo->$key);
+      }
+      $this->assertEqual($val, $db_repo->$key, t('%key repository attribute is stored correctly', array('%key' => $key)), t('Creation'));
+    }
+  }
+
+  //FIXME use the controller directly
+  public function testRead() {
+    $controller = new VersioncontrolRepositoryController();
+    // no repo, test get get valid output
+    $repositories = $controller->load(array(''));
+    $this->assertTrue(empty($repositories), t('Make sure we get an empty array when trying to get a repository with empty repo_id'), t('Read'));
+
+    $repositories = $controller->load(NULL);
+    $this->assertTrue(empty($repositories), t('Make sure we get an empty array when trying to get a repository with a NULL repo_id'), t('Read'));
+
+    $repositories = $controller->load(array(1));
+    $this->assertTrue(empty($repositories), t('Make sure we get an empty array when trying to get a repository which do not exit'), t('Read'));
+
+    // create repo and test again
+    $repo = $this->versioncontrolCreateRepository('test');
+    $repositories = $controller->load(array($repo->repo_id));
+    $this->assertEqual(count($repositories), 1, t('Only one repository found'), t('Read'));
+    $repository = reset($repositories);
+    $this->assertTrue(isset($repository) && is_a($repository, 'VersioncontrolRepository'), t('Make sure we get a valid return when passing a good repo_id.'), t('Read'));
+    $this->assertEqual($repository->repo_id, $repo->repo_id, t('Make sure we get the right repository.'), t('Read'));
+  }
+
+  public function testUpdate() {
+    $repo = $this->versioncontrolCreateRepository('test');
+
+    $data = array(
+      'name' => $this->randomName(),
+      'root' => '/updated/path',
+      'updated' => time(),
+      //TODO $repo->update_method
+      'locked' => TRUE,
+      'plugins' => array('auth_handler' => 'account') + $repo['plugins'],
+    );
+
+    $repo->name = $data['name'];
+    $repo->root = $data['root'];
+    $repo->updated = $data['updated'];
+    //TODO $repo->update_method
+    $repo->locked = $data['locked'];
+    $repo->plugins = $data['plugins'];
+    $repo->update();
+
+    $db_repo = db_select('versioncontrol_repositories', 'r')
+      ->fields('r')
+      ->condition('repo_id', $repo->repo_id)
+      ->execute()
+      ->fetchObject();
+    foreach ($data as $key => $val) {
+      if ($key == 'data' || $key == 'plugins') {
+        $db_repo->$key = unserialize($db_repo->$key);
+      }
+      $this->assertEqual($val, $db_repo->$key, t('%key repository attribute is updated correctly', array('%key' => $key)), t('Update'));
+    }
+
   }
+
+  public function testDelete() {
+    $repo = $this->versioncontrolCreateRepository('test');
+    $repo->delete();
+
+    $result = db_select('versioncontrol_repositories', 'r')
+      ->fields('r')
+      ->condition('repo_id', $repo->repo_id)
+      ->execute();
+    foreach ($result as $db_repo) {
+      $this->fail(t('The repository was not deleted correctly.'), t('Delete'));
+    }
+  }
+
 }
diff --git tests/VersioncontrolTestCase.test tests/VersioncontrolTestCase.test
index 7eba0cc..cc60ff5 100644
--- tests/VersioncontrolTestCase.test
+++ tests/VersioncontrolTestCase.test
@@ -138,14 +138,17 @@ abstract class VersioncontrolTestCase extends DrupalWebTestCase {
       'name' => 'test_repo_' . ++$i,
       'vcs' => $backend_name,
       'root' => '/fake/path/to/repo',
-      'update_method' => 0,
+      'authorization_method' => 'versioncontrol_admin',
       'updated' => 0,
+      'update_method' => 0,
       'locked' => 0,
       'data' => array(),
       'plugins' => array(),
     );
     $default_plugins = array(
       'auth_handler' => 'ffa',
+      'author_mapper' => 'simple_mail',
+      'committer_mapper' => 'simple_mail',
     );
 
     $data += $default_data;
@@ -157,6 +160,7 @@ abstract class VersioncontrolTestCase extends DrupalWebTestCase {
     $backend = $this->backends[$backend_name];
     $repo = $backend->buildEntity('repo', $data);
     $repo->insert();
+    $this->assertTrue(isset($repo->repo_id) && is_numeric($repo->repo_id), t('VersioncontrolRepository::insert() properly populates a new repository object with an integer repo_id.'));
 
     return $repo;
   }
-- 
1.7.2.3

