示例#1
0
    public function test_term_tree()
    {
        // create a vocabulary.
        if (Vocabulary::get('format_test')) {
            Vocabulary::get('format_test')->delete();
        }
        $v = Vocabulary::create(array('name' => 'format_test', 'description' => "Vocabulary used for testing Format::term_tree()", 'features' => array('hierarchical')));
        // nest some terms.
        /**
         * A
         * | \
         * B  C
         * |  | \
         * D  E  F 		// E has no descendants!
         * | \   | \
         * G  H  I  J	// G has no descendants!
         *   / \  \   \
         *  K   L  M   N
         **/
        $a = $v->add_term("A");
        $b = $v->add_term("B", $v->get_term($a->id));
        $c = $v->add_term("C", $v->get_term($a->id));
        $d = $v->add_term("D", $v->get_term($b->id));
        $e = $v->add_term("E", $v->get_term($c->id));
        $f = $v->add_term("F", $v->get_term($c->id));
        $g = $v->add_term("G", $v->get_term($d->id));
        $h = $v->add_term("H", $v->get_term($d->id));
        $i = $v->add_term("I", $v->get_term($f->id));
        $j = $v->add_term("J", $v->get_term($f->id));
        $k = $v->add_term("K", $v->get_term($h->id));
        $l = $v->add_term("L", $v->get_term($h->id));
        $m = $v->add_term("M", $v->get_term($i->id));
        $n = $v->add_term("N", $v->get_term($j->id));
        $tree_output = Format::term_tree($v->get_tree(), 'test', array('itemstart' => '<li>', 'itemattr' => '', 'liststart' => '<ol>', 'wrapper' => '%s'));
        $expected_output = <<<END
<ol class="tree" id="tree_test"><li>A<ol><li>B<ol><li>D<ol><li>G</li>
<li>H<ol><li>K</li>
<li>L</li>
</ol></li>
</ol></li>
</ol></li>
<li>C<ol><li>E</li>
<li>F<ol><li>I<ol><li>M</li>
</ol></li>
<li>J<ol><li>N</li>
</ol></li>
</ol></li>
</ol></li>
</ol></li>
</ol>
END;
        $result = $tree_output === $expected_output;
        if (!$result) {
            $this->output(sprintf('<strong>Expected:</strong><br><textarea rows="16">%s</textarea><br><strong>Got:</strong><br><textarea rows="16">%s</textarea>', $expected_output, $tree_output));
        }
        $this->assert_true($result, "Output does not match desired HTML");
        // clean up
        $v->delete();
    }
 /**
  * Add the category vocabulary and create the admin token
  *
  **/
 public function action_plugin_activation($file)
 {
     $params = array('name' => self::$vocabulary, 'description' => 'A vocabulary for describing Categories', 'features' => array('multiple', 'hierarchical'));
     Vocabulary::create($params);
     // create default access token
     ACL::create_token('manage_categories', _t('Manage categories'), 'Administration', false);
     $group = UserGroup::get_by_name('admin');
     $group->grant('manage_categories');
 }
示例#3
0
 /**
  * Add the shelf vocabulary and create the admin token
  *
  **/
 public function action_plugin_activation($file)
 {
     if (Plugins::id_from_file($file) == Plugins::id_from_file(__FILE__)) {
         $params = array('name' => self::$vocabulary, 'description' => 'A vocabulary for describing Shelves', 'features' => array('hierarchical'));
         Vocabulary::create($params);
         // create default access token
         ACL::create_token('manage_shelves', _t('Manage ') . Options::get('shelves__plural', _t('shelves', 'shelves')), 'Administration', false);
         $group = UserGroup::get_by_name('admin');
         $group->grant('manage_shelves');
     }
 }
 /**
  * Hook on activation of this plugin
  */
 public function action_plugin_activation()
 {
     // add the new content types
     Post::add_new_type('addon');
     // allow reading the new content types
     UserGroup::get_by_name('anonymous')->grant('post_addon', 'read');
     // create a permissions token
     ACL::create_token('manage_versions', _t('Manage Addon Versions', 'addon_catalog'), 'Addon Catalog', false);
     // create the addon vocabulary (type)
     Vocabulary::add_object_type('addon');
     // create the addon vocabulary
     $params = array('name' => self::CATALOG_VOCABULARY, 'description' => _t('A vocabulary for addon versions in the addons catalog', 'addon_catalog'));
     $vocabulary = Vocabulary::create($params);
     // @TODO: notification/log of some sort?
     // create the default content
     include 'create_core_addons.php';
 }
 public function add_menu_form_save($form)
 {
     $params = array('name' => $form->menuname->value, 'description' => $form->description->value, 'features' => array('term_menu', 'unique'));
     $vocab = Vocabulary::create($params);
     Utils::redirect(URL::get('admin', array('page' => 'menus', 'action' => 'edit', 'menu' => $vocab->id)));
 }
示例#6
0
 private function upgrade_db_post_3749()
 {
     $type_id = Vocabulary::object_type_id('post');
     $vocabulary = Vocabulary::create(array('name' => 'tags', 'description' => 'Habari\'s tags implementation', 'features' => array('multiple', 'free')));
     $new_tag = null;
     $post_ids = array();
     $prefix = Config::get('db_connection')->prefix;
     $results = DB::get_results("SELECT id, tag_text, tag_slug from {$prefix}tags");
     foreach ($results as $tag) {
         $new_tag = $vocabulary->add_term($tag->tag_text);
         $post_ids = DB::get_column("SELECT post_id FROM {$prefix}tag2post WHERE tag_id = ?", array($tag->id));
         foreach ($post_ids as $id) {
             DB::insert("{object_terms}", array('term_id' => $new_tag->id, 'object_id' => $id, 'object_type_id' => $type_id));
         }
     }
 }
示例#7
0
 /**
  * Get posts by vocabulary
  * - vocabulary => an array describing parameters related to vocabularies attached to posts. This can be one of two forms:
  *   - object-based, in which an array of Term objects are passed
  *     - any => posts associated with any of the terms are returned
  *     - all => posts associated with all of the terms are returned
  *     - not => posts associated with none of the terms are returned
  *   - property-based, in which an array of vocabulary names and associated fields are passed
  *     - vocabulary_name:term => a vocabulary name and term slug pair or array of vocabulary name and term slug pairs, any of which can be associated with the posts
  *     - vocabulary_name:term_display => a vocabulary name and term display pair or array of vocabulary name and term display pairs, any of which can be associated with the posts
  *     - vocabulary_name:not:term => a vocabulary name and term slug pair or array of vocabulary name and term slug pairs, none of which can be associated with the posts
  *     - vocabulary_name:not:term_display => a vocabulary name and term display pair or array of vocabulary name and term display pairs, none of which can be associated with the posts
  *     - vocabulary_name:all:term => a vocabulary name and term slug pair or array of vocabulary name and term slug pairs, all of which must be associated with the posts
  *     - vocabulary_name:all:term_display => a vocabulary name and term display pair or array of vocabulary name and term display pairs, all of which must be associated with the posts
  */
 public function test_get_posts_by_vocabulary()
 {
     // setup
     // create a couple Vocabularies and Terms
     if (Vocabulary::get("fizz")) {
         Vocabulary::get("fizz")->delete();
     }
     $fizz = Vocabulary::create(array('name' => 'fizz', 'description' => 'Vocabulary for Posts testing.', 'features' => array('free')));
     $fizz_term = new Term(array('term' => 'fizz', 'term_display' => 'Fizz'));
     $fizz->add_term($fizz_term);
     $extra_fizz_term = new Term(array('term' => 'extra fizzy', 'term_display' => 'Extra Fizzy'));
     $fizz->add_term($extra_fizz_term);
     if (Vocabulary::get("buzz")) {
         Vocabulary::get("buzz")->delete();
     }
     $buzz = Vocabulary::create(array('name' => 'buzz', 'description' => 'Another Vocabulary for Posts testing.', 'features' => array('free')));
     $buzz_term = new Term(array('term' => 'buzz', 'term_display' => 'Buzz'));
     $buzz->add_term($buzz_term);
     // create some Posts and associate them with the two Vocabularies
     for ($i = 1; $i <= 20; $i++) {
         $post = Post::create(array('title' => "Test Post {$i}", 'content' => 'If this were really a post...', 'user_id' => $this->user->id, 'status' => Post::status('published'), 'content_type' => Post::type('entry'), 'pubdate' => DateTime::date_create(time())));
         $post->info->testing_vocab = 1;
         $post->info->i = $i;
         $post->info->commit();
         if ($i % 3 === 0) {
             $fizz->set_object_terms('post', $post->id, array($fizz_term->term));
         }
         if ($i % 5 === 0) {
             $buzz->set_object_terms('post', $post->id, array($buzz_term->term));
         }
     }
     // Object-based syntax
     $total_posts = Posts::count_total();
     $any_vocab_posts = Posts::get(array('ignore_permissions' => true, 'vocabulary' => array("any" => array($fizz_term, $buzz_term)), 'nolimit' => 1, 'count' => 'DISTINCT {posts}.id'));
     $all_vocab_posts = Posts::get(array('ignore_permissions' => true, 'vocabulary' => array("all" => array($fizz_term, $buzz_term)), 'nolimit' => 1, 'count' => 'DISTINCT {posts}.id'));
     $not_vocab_posts = Posts::get(array('ignore_permissions' => true, 'vocabulary' => array("not" => array($fizz_term, $buzz_term)), 'nolimit' => 1, 'count' => 'DISTINCT {posts}.id'));
     $this->assert_true($any_vocab_posts > $all_vocab_posts, "Any: {$any_vocab_posts} should be greater than All: {$all_vocab_posts}");
     $this->assert_true($not_vocab_posts > $all_vocab_posts, "Not: {$not_vocab_posts} should be greater than All: {$all_vocab_posts}");
     $this->assert_true($not_vocab_posts < $total_posts, "Not: {$not_vocab_posts} should be less than Total: {$total_posts}");
     $this->assert_equal($any_vocab_posts + $not_vocab_posts, $total_posts, "Any: {$any_vocab_posts} plus Not: {$not_vocab_posts} should equal Total: {$total_posts}");
     // Property-based syntax
     $any_vocab_posts = Posts::get(array('ignore_permissions' => true, 'vocabulary' => array("fizz:term" => "fizz", "buzz:term" => "buzz"), 'nolimit' => 1, 'count' => 'DISTINCT {posts}.id'));
     $all_vocab_posts = Posts::get(array('ignore_permissions' => true, 'vocabulary' => array("fizz:all:term" => "fizz", "buzz:all:term" => "buzz"), 'nolimit' => 1, 'count' => 'DISTINCT {posts}.id'));
     $not_vocab_posts = Posts::get(array('ignore_permissions' => true, 'vocabulary' => array("fizz:not:term" => "fizz", "buzz:not:term" => "buzz"), 'nolimit' => 1, 'count' => 'DISTINCT {posts}.id'));
     $this->assert_true($any_vocab_posts > $all_vocab_posts, "Any: {$any_vocab_posts} should be greater than All: {$all_vocab_posts}");
     $this->assert_true($not_vocab_posts > $all_vocab_posts, "Not: {$not_vocab_posts} should be greater than All: {$all_vocab_posts}");
     $this->assert_true($not_vocab_posts < $total_posts, "Not: {$not_vocab_posts} should be less than Total: {$total_posts}");
     $this->assert_equal($any_vocab_posts + $not_vocab_posts, $total_posts, "Any: {$any_vocab_posts} plus Not: {$not_vocab_posts} should equal Total: {$total_posts}");
     // teardown
     Posts::get(array('ignore_permissions' => true, 'has:info' => 'testing_vocab', 'nolimit' => 1))->delete();
     $fizz->delete();
     $buzz->delete();
 }
示例#8
0
	public function test_object_terms()
	{
		$post = Post::create( array(
		'title' => 'Unit Test Post',
		'content' => 'This is a unit test post to test setting and getting terms.',
		'user_id' => 1,
		'status' => Post::status( 'draft' ),
		'content_type' => Post::type( 'entry' ),
		'pubdate' => HabariDateTime::date_create(),
		) );

		$v = Vocabulary::get( 'tags' );

		// Test setting terms with strings
		$new_terms = array( 'habari', 'unit test' );
		$v->set_object_terms( 'post', $post->id, $new_terms );
		$terms = $v->get_object_terms( 'post', $post->id );
		$t = array();
		foreach( $terms as $term ) {
			$t[] = (string)$term;
		}

		$this->assert_equal( 2, count( $terms ) );
		$this->assert_equal( 0, count( array_diff( $new_terms, $t ) ) );

		// Test get_all_object_terms
		$nv = Vocabulary::create( array(
		'name' => 'animals',
		'description' => 'Types of animals.',
		'features' => array( 'hierarchical' )
		) );

		$root = $nv->add_term( 'Animal Kingdom' );
		$nv->set_object_terms( 'post', $post->id, array( $root ) );

		$terms = Vocabulary::get_all_object_terms( 'post', $post->id );
		$new_terms[] = 'Animal Kingdom';
		$t = array();
		foreach( $terms as $term ) {
			$t[] = (string)$term;
		}

		$this->assert_equal( 3, count( $terms ) );
Utils::debug( 3, count( $terms ) );
		$this->assert_equal( 0, count( array_diff( $new_terms, $t ) ) );
Utils::debug( 0, count( array_diff( $new_terms, $t ) ) );

		$v->delete_term( 'unit test' );
		$post->delete();
		$nv->delete();
	}