view og_statistics.test @ 0:9ce879ecbce6

OG Stats beta 3
author Franck Deroche <franck@defr.org>
date Tue, 24 Nov 2009 14:25:13 +0100
parents
children 48f07e7acaca
line wrap: on
line source
<?php
// $Id: og_statistics.test,v 1.2.2.2 2009/06/05 22:38:12 dereine Exp $

/**
 * Test file for og_statitics.
 *
 * @TODO Use multiple groups for testing.
 * @TODO Abstract part of the tests.
 */

require_once drupal_get_path('module', 'og') .'/tests/og_testcase.php';

class OgStatisticsTestCase extends OgTestCase {
  function getInfo() {
    return array(
      'name' => t('Organic groups statistics'),
      'description' => t('tests statistics of og_statistics module'),
      'group' => t('Organic groups'),
    );
  }

  function setUp() {
    parent::setUp('node', 'user', 'comment', 'views', 'og', 'og_views', 'og_statistics', 'og_access');

    // Create a user with admin permissions.
    $web_admin = $this->drupalCreateUser(array('administer nodes', 'administer content types', 'access administration pages', 'administer site configuration', 'administer organic groups'));
    $this->drupalLogin($web_admin);
  }

  function TestOgStatisticsWritePureRecord() {
    // Create a group node content type.
    $og_group_type = $this->drupalCreateContentType();
    variable_set('og_content_type_usage_'. $og_group_type->name, 'group');

    // Create a group post content type.
    $og_post_type = $this->drupalCreateContentType();
    variable_set('og_content_type_usage_'. $og_post_type->name, 'group_post_standard');

    // Rebuild the menu so the new content types will appear in the menu.
    menu_rebuild();

    // Create a group node.
    $gid = $this->addOgGroup($og_group_type->name);

    // get the record of the group
    $stat = og_statistics_load($gid);
    unset($stat['nid']);
    $members = 

    $empty = FALSE;
    $this->assertFalse($empty, print_r($stat, TRUE));
    foreach ($stat as $key => $item) {
      $empty = $empty || $item == TRUE;
    }
    $this->assertFalse($empty, t('All stats should have 0 as item'));
  }

  // Tests nodeapi statistics.
  function TestOgStatisticsNodeapi() {
    // Create a group node content type.
    $og_group_type = $this->drupalCreateContentType();
    variable_set('og_content_type_usage_'. $og_group_type->name, 'group');

    // Create a group post content type.
    $og_post_type = $this->drupalCreateContentType();
    variable_set('og_content_type_usage_'. $og_post_type->name, 'group_post_standard');

    // Rebuild the menu so the new content types will appear in the menu.
    menu_rebuild();

    // create a test group
    $gid = $this->addOgGroup($og_group_type->name);
    $before = og_statistics_load($gid);

    // add a test post
    $nids[] = $nid = $this->addOgPost($og_post_type->name, array($gid));
    $node = node_load($nid);
    // Rebuild the menu so the new content types will appear in the menu.
    menu_rebuild();
    $after = og_statistics_load($gid);
    $count_one_up = ($after['posts_count'] - $before['posts_count']) == 1;

    $this->assertTrue($count_one_up, 'A node post in a group, counts the counter 1 up');
    $this->assertEqual($node->created, $after['last_node_timestamp'], 'A node post in a group, sets the last node timestamp');

    // remove the post
    node_delete($nid);
    $before = $after;
    $after = og_statistics_load($gid);
    $count_down = ($after['posts_count'] - $before['posts_count']) == -1;

    $this->assertTrue($count_down, 'A node poast in a group was deleted, counts the counter 1 down');
  }

  // Tests comments hooks statistic
  function TestOgStatisticsComment() {
    // Create a group node content type.
    $og_group_type = $this->drupalCreateContentType();
    variable_set('og_content_type_usage_'. $og_group_type->name, 'group');

    // Create a group post content type.
    $og_post_type = $this->drupalCreateContentType();
    variable_set('og_content_type_usage_'. $og_post_type->name, 'group_post_standard');

    // Rebuild the menu so the new content types will appear in the menu.
    menu_rebuild();

    // create a test group
    $gid = $this->addOgGroup($og_group_type->name);
    $before = og_statistics_load($gid);

    // add a test post
    $nids[] = $nid = $this->addOgPost($og_post_type->name, array($gid));

    // adds some comments
    $edit = array();
    $edit['subject'] = $this->randomName(10);
    $edit['comment'] = $this->randomName(50);
    $edit['nid'] = $nid;
    $cid = comment_save($edit);
    $comment = _comment_load($cid);

    $after = og_statistics_load($gid);
    $count_up = ($after['comments_count'] - $before['comments_count']) == 1;
    $this->assertTrue($count_up, 'new comment to one group post, counts up the comment counter');
    $this->assertEqual($comment->timestamp, $after['last_comment_timestamp'], 'new comment to one group post, counts up the comment counter');
  }

  function TestOgStatisticsOg() {
    // Create a group node content type.
    $og_group_type = $this->drupalCreateContentType();
    variable_set('og_content_type_usage_'. $og_group_type->name, 'group');

    // Create a group post content type.
    $og_post_type = $this->drupalCreateContentType();
    variable_set('og_content_type_usage_'. $og_post_type->name, 'group_post_standard');

    // Rebuild the menu so the new content types will appear in the menu.
    menu_rebuild();

    // create a test group
    $gid = $this->addOgGroup($og_group_type->name);
    $before = og_statistics_load($gid);

    // let one member choine the group
    $account = $this->drupalCreateUser();
    $time = time();
    og_save_subscription($gid, $account->uid, array('created' => $time));

    $after = og_statistics_load($gid);

    $count_up = ($after['members_count'] - $before['members_count']) == 1;

    $this->assertTrue($count_up, 'New subscription to a group adds the counter 1 up');
    $this->assertEqual($time, $after['last_member_timestamp'], 'New subscription to a group adds the counter 1 up');
  }
}