? modules/blogapi/blogapi56.test
? modules/blogapi/blogapi56.txt
Index: modules/blogapi/blogapi.test
===================================================================
RCS file: /cvs/drupal/drupal/modules/blogapi/blogapi.test,v
retrieving revision 1.5
diff -u -p -r1.5 blogapi.test
--- modules/blogapi/blogapi.test	19 Sep 2008 20:25:02 -0000	1.5
+++ modules/blogapi/blogapi.test	10 Oct 2008 01:43:30 -0000
@@ -24,16 +24,14 @@ class BlogAPITestCase extends DrupalWebT
    * Create, edit, and delete post; upload file; set/get categories.
    */
   function testBlogAPI() {
-    // Create admin user and taxononmy for later use.
+    // Create user.
+    $web_user = $this->drupalCreateUser(array('create blog content', 'delete own blog content', 'edit own blog content', 'administer content with blog api'));
+    // Create admin user and taxonomy for later use.
     $admin_user = $this->drupalCreateUser(array('administer taxonomy'));
     $this->drupalLogin($admin_user);
     $vid = $this->addVocabulary('simpletest_vocab');
-    $term = $this->addTerm($vid, 'simpletest_term1');
-    $this->drupalLogout();
-
-    // Create user.
-    $web_user = $this->drupalCreateUser(array('create blog content', 'delete own blog content', 'edit own blog content', 'administer content with blog api'));
-    $this->drupalLogin($web_user);
+    $term_1 = $this->addTerm($vid, 'simpletest_term1');
+    $term_2 = $this->addTerm($vid, 'simpletest_term2');
 
     // Init common variables.
     $local = url('xmlrpc.php', array('absolute' => TRUE));
@@ -63,8 +61,9 @@ class BlogAPITestCase extends DrupalWebT
     if ($result !== FALSE && array_key_exists('title', $result[0])) {
       $this->assertEqual($content, $result[0]['title'], t('Post found.'));
     }
-    else
-      $this->assertTrue(false, 'Post found.');
+    else {
+      $this->fail(t('Post found.'));
+    }
 
     // Edit post.
     $content_new = $this->randomName(10);
@@ -88,7 +87,7 @@ class BlogAPITestCase extends DrupalWebT
     $this->assertEqual($this->drupalGetContent(), $file_contents, t('Uploaded contents verified.'));
 
     // Set post categories.
-    $categories = array(array('categoryId' => $term));
+    $categories = array(array('categoryId' => $term_1));
     $result = xmlrpc($local, 'mt.setPostCategories', $nid, $web_user->name, $web_user->pass_raw, $categories);
     $this->assertTrue($result, t('Post categories set.'));
 
@@ -96,10 +95,35 @@ class BlogAPITestCase extends DrupalWebT
     $result = xmlrpc($local, 'mt.getPostCategories', $nid, $web_user->name, $web_user->pass_raw);
     $this->assertTrue($result, t('Category list successfully retreived.'));
 
-    if ($result !== FALSE && array_key_exists('categoryId', $result[0])) {
-      $this->assertEqual($term, $result[0]['categoryId'], t('Category list verified.'));
+    if ($result !== FALSE && isset($result[0]['categoryId'])) {
+      $this->assertEqual($term_1, $result[0]['categoryId'], t('Category list verified.'));
     }
-
+    // Test validation of category assignment.
+    // Set post categories.
+    $categories[] = array('categoryId' => $term_2);
+    $result = xmlrpc($local, 'mt.setPostCategories', $nid, $web_user->name, $web_user->pass_raw, $categories);
+    $this->assertFalse($result, t('Post categories fail validation (attempt to post two when one is allowed).'));
+    // Change to required.
+    $this->updateVocabulary($vid, 'simpletest_vocab1', FALSE, TRUE);
+    $result = xmlrpc($local, 'mt.setPostCategories', $nid, $web_user->name, $web_user->pass_raw, array());
+    $this->assertFalse($result, t("Post categories fail validation (none posted when it's required)."));
+    // Change to allowing multiple, not required.
+    $this->updateVocabulary($vid, 'simpletest_vocab1', TRUE, FALSE);
+    $result = xmlrpc($local, 'mt.setPostCategories', $nid, $web_user->name, $web_user->pass_raw, $categories);
+    $this->assertTrue($result, t('Post categories pass validation (multiple allowed).'));
+    $result = xmlrpc($local, 'mt.setPostCategories', $nid, $web_user->name, $web_user->pass_raw, array());
+    $this->assertTrue($result, t('Post categories pass validation (multiple allowed, none posted).'));
+    // Change to multiple, but required.
+    $this->updateVocabulary($vid, 'simpletest_vocab1', TRUE, TRUE);
+    $result = xmlrpc($local, 'mt.setPostCategories', $nid, $web_user->name, $web_user->pass_raw, $categories);
+    $this->assertTrue($result, t('Post categories pass validation (multiple allowed).'));
+    $result = xmlrpc($local, 'mt.setPostCategories', $nid, $web_user->name, $web_user->pass_raw, array());
+    $this->assertFalse($result, t("Post categories fail validation (none posted when it's required)."));
+    // Try to add a non-existent term.
+    $categories[] = array('categoryId' => $term_2 + 1);
+    $result = xmlrpc($local, 'mt.setPostCategories', $nid, $web_user->name, $web_user->pass_raw, $categories);
+    $this->assertFalse($result, t('Post categories fail validation (unknown term).'));
+ 
     // Delete post.
     $result = xmlrpc($local, 'blogger.deletePost', $appid, $nid, $web_user->name, $web_user->pass_raw, TRUE);
     $this->assertTrue($result, t('Post successfully deleted.'));
@@ -113,14 +137,16 @@ class BlogAPITestCase extends DrupalWebT
    * @return integer
    *   The vocab ID.
    */
-  function addVocabulary($vocab) {
+  function addVocabulary($vocab, $multiple = FALSE, $required = FALSE) {
     $edit = array();
     $edit['name'] = $vocab;
     $edit['nodes[blog]'] = TRUE;
+    $edit['multiple'] = $multiple;
+    $edit['required'] = $required;
     $this->drupalPost('admin/content/taxonomy/add', $edit, t('Save'));
     $this->assertRaw(t('Created new vocabulary %vocab.', array('%vocab' => $edit['name'])), t('Taxonomy vocabulary added.'));
 
-    $vocab_arr = taxonomy_get_vocabularies();
+    $vocab_arr = taxonomy_get_vocabularies('blog');
     $vid = NULL;
     foreach ($vocab_arr as $vocab_item) {
       if ($vocab_item->name == $vocab) {
@@ -133,6 +159,23 @@ class BlogAPITestCase extends DrupalWebT
 
     return $vid;
   }
+  /**
+   * Update a taxonomy vocabulary.
+   *
+   * @param  $vocab
+   *   Vocabulary name.
+   * @return integer
+   *   The vocab ID.
+   */
+  function updateVocabulary($vid, $vocab, $multiple = FALSE, $required = FALSE) {
+    $edit = array();
+    $edit['name'] = $vocab;
+    $edit['nodes[blog]'] = TRUE;
+    $edit['multiple'] = $multiple;
+    $edit['required'] = $required;
+    $this->drupalPost('admin/content/taxonomy/' . $vid, $edit, t('Save'));
+    $this->assertRaw(t('Updated vocabulary %vocab.', array('%vocab' => $edit['name'])), t('Taxonomy vocabulary changed.'));
+  }
 
   /**
    * Add a taxonomy term to vocabulary.
@@ -149,16 +192,9 @@ class BlogAPITestCase extends DrupalWebT
     $this->drupalPost('admin/content/taxonomy/' . $vid . '/add', $edit, t('Save'));
     $this->assertRaw(t('Created new term %term.', array('%term' => $edit['name'])), t('Taxonomy term added.'));
 
-    $tree = taxonomy_get_tree($vid);
-    $tid = NULL;
-    foreach ($tree as $tree_term) {
-      if ($tree_term->name == $term) {
-        $tid = $tree_term->tid;
-        break;
-      }
-    }
+    $tid = db_result(db_query("SELECT t.tid FROM {term_data} t  WHERE t.vid = %d AND t.name = '%s'", array($vid, $term)));
 
-    $this->assertNotNull($tid, t('Term found in database.'));
+    $this->assertTrue((bool)$tid, t('Term found in database.'));
 
     return $tid;
   }
