[ Index ]
 

Code source de LifeType 1.2.4

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/class/test/helpers/ -> testtools.class.php (source)

   1  <?php
   2  
   3      lt_include( PLOG_CLASS_PATH."class/dao/blogs.class.php" );
   4      lt_include( PLOG_CLASS_PATH."class/dao/bloginfo.class.php" );        
   5      lt_include( PLOG_CLASS_PATH."class/dao/articles.class.php" );    
   6      lt_include( PLOG_CLASS_PATH."class/dao/article.class.php" );        
   7      lt_include( PLOG_CLASS_PATH."class/dao/users.class.php" );
   8      lt_include( PLOG_CLASS_PATH."class/dao/userinfo.class.php" );    
   9      lt_include( PLOG_CLASS_PATH."class/dao/articlecategories.class.php" );
  10      lt_include( PLOG_CLASS_PATH."class/dao/articlecategory.class.php" );        
  11  
  12      /**
  13       * \ingroup Test
  14       *
  15       * Several used methods that are used throughout the test cases
  16       */
  17      class TestTools
  18      {
  19          /** 
  20           * Creates a temporary blog
  21           *
  22           * @param ownerId Id of the owner
  23           * @return A BlogInfo object if successful or false otherwise
  24           */
  25  		function createBlog( $ownerId )
  26          {
  27              $blog = new BlogInfo(
  28                  "Test blog ".md5(time()),
  29                  $ownerId,
  30                  "About test blog",
  31                  ""
  32              );
  33              
  34              $blogs = new Blogs();
  35              if( $blogs->addBlog( $blog ))
  36                  return $blog;
  37              else
  38                  return false;
  39          }
  40          
  41          /**
  42           * Creates a temporary user
  43           *
  44           * @return A UserInfo object if successful or false otherwise
  45           */
  46  		function createUser()
  47          {
  48              $user = new UserInfo(
  49                  TestTools::getRandomWord( 15 ),
  50                  "password",
  51                  "test@user.com",
  52                  "About test user",
  53                  "Test User"                
  54              );
  55              
  56              $users = new Users();
  57              if( $users->addUser( $user ))
  58                  return( $user );
  59              else
  60                  return( false );
  61          }
  62          
  63          /**
  64           * Creates a temporary admin user
  65           *
  66           * @return A UserInfo object if successful or false otherwise
  67           */
  68  		function createAdminUser()
  69          {
  70              $user = new UserInfo(
  71                  TestTools::getRandomWord( 15 ),
  72                  "password",
  73                  "test@user.com",
  74                  "About test user",
  75                  "Test User"                
  76              );
  77              $user->setSiteAdmin( true );
  78              
  79              $users = new Users();
  80              if( $users->addUser( $user ))
  81                  return( $user );
  82              else
  83                  return( false );
  84          }        
  85          
  86          /**
  87           * Create a temporary article
  88           *
  89           * @param blogId Id of the blog to which this article belongs
  90           * @param categories An array with category ids
  91           * @param status A valid status for the article, POST_STATUS_PUBLISHED if none specified
  92           * @param date A Timestamp, the current date will be used if none specified
  93           * @return An Article object if successful or false otherwise
  94           */
  95  		function createArticle( $blogId, $userId, $categoryIds, $status = POST_STATUS_PUBLISHED, $date = null )
  96          {
  97              $article = new Article(
  98                  "Topic of test article",
  99                  "Text of test article",
 100                  $categoryIds,
 101                  $userId,
 102                  $blogId,
 103                  $status,
 104                  0                
 105              );
 106              if( $date != null ) {
 107                  $article->setDateObject( $date );
 108              }
 109              
 110              $articles = new Articles();
 111              if( $articles->addArticle( $article ))
 112                  return( $article );
 113              else
 114                  return( false );
 115          }
 116          
 117          /**
 118           * Create a temporary article category
 119           *
 120           * @param blogId 
 121           * @return An ArticleCategory object or false otherwise
 122           */
 123  		function createArticleCategory( $blogId )
 124          {
 125              $cat = new ArticleCategory(
 126                  "Test category ".md5(rand()),
 127                  "",
 128                  $blogId,
 129                  true
 130              );
 131              
 132              $cats = new ArticleCategories();
 133              if( $cats->addArticleCategory( $cat )) 
 134                  return( $cat );
 135              else
 136                  return( false );
 137          }
 138          
 139          /**
 140           * Creates a clean scenario for tests, including one blog, one user, one or more different articles and
 141           * one or more different article categories
 142           *
 143           * @param params An array
 144           * @return Returns an array with several different fields containing the user (owner), blog, categories
 145           * and articles.
 146           */
 147  		function createBlogScenario( $params = Array())
 148          {
 149              $numArticles = isset( $params["num_articles"] ) ? $params["num_articles"] : 1;
 150              $numCategories = isset( $params["num_categories"] ) ? $params["num_categories"] : 1;
 151              
 152              // create the user
 153              $user = TestTools::createUser();
 154              // create the blog
 155              $blog = TestTools::createBlog( $user->getId());
 156              // create the categories
 157              $i = 0;
 158              $categories = Array();
 159              while( $i < $numCategories ) {
 160                  $categories[$i] = TestTools::createArticleCategory( $blog->getId());
 161                  $i++;                
 162              }
 163              // create the articles
 164              $i = 0;
 165  
 166              while( $i < $numArticles ) {
 167                  // select the categories
 168                  if( $numCategories == 1 )
 169                      $catIds = Array( $categories[0]->getId());
 170                  else {
 171                      // pick a random number between 1 and $numCategories
 172                      $maxNum = rand( 1, $numCategories );
 173                      $j = 0;
 174                      while( $j < $maxNum ) {
 175                          $pos = rand( 0, $numCategories-1 );
 176                          $catIds[] = $categories[$pos]->getId();
 177                          $j++;
 178                      }
 179                  }
 180                  $articles[$i] = TestTools::createArticle( $blog->getId(), $user->getId(), $catIds, POST_STATUS_PUBLISHED );
 181                  $i++;
 182                  $catIds = Array();
 183              }
 184              
 185              $result = Array();
 186              $result["user"] = $user;
 187              $result["blog"] = $blog;
 188              $i = 0;
 189              // need to reload the category so that the article counters are correct
 190              $articleCategories = new ArticleCategories();
 191              foreach( $categories as $cat ) {
 192                  $result["categories"][$i] = $articleCategories->getCategory( $cat->getId());
 193                  $i++;                
 194              }
 195              $result["articles"] = $articles;
 196              
 197              return( $result );
 198          }
 199          
 200          /**
 201           * Deletes any test data created
 202           *
 203           * @param data An array with DAO objects
 204           */
 205  		function deleteDaoTestData( $data )
 206          {
 207              foreach( $data as $item ) {
 208                  // check the item class and act accordingly
 209                  $className = strtolower( get_class( $item ));
 210                  if( $className == "article" ) {
 211                      $articles = new Articles();
 212                      $articles->deleteArticle( $item->getId(), $item->getUserId(), $item->getBlogId(), true );
 213                  }
 214                  elseif( $className == "bloginfo" ) {
 215                      $blogs = new Blogs();
 216                      $blogs->deleteBlog( $item->getId());
 217                  }
 218                  elseif( $className == "articlecategory" ) {
 219                      $cats = new ArticleCategories();
 220                      $cats->deleteCategory( $item->getId(), $item->getBlogId());
 221                  }
 222                  elseif( $className == "userinfo" ) {
 223                      $users = new Users();
 224                      $users->deleteUser( $item->getId());
 225                  }
 226                  elseif( $className == "usercomment" ) {
 227                      $comments = new ArticleComments();
 228                      $comments->deleteComment( $item->getId());
 229                  }
 230                  else {
 231                      print("Unrecognized object of class $className" );
 232                      print_r( $item );
 233                      die();
 234                  }
 235              }
 236          }
 237          
 238          /**
 239           * Generate random words
 240           */
 241  		function getRandomWord($lenght, $uppercase = false, $html = true) 
 242          {
 243              $newcode_length = 1;
 244              $newcode = "";
 245              while($newcode_length < $lenght) {
 246                  $a=97;
 247                  $b=122;
 248                  if ($newcode_length == 1) {
 249                      if (rand(1,4) == 1 || $uppercase) {
 250                          $a=65;
 251                          $b=90;
 252                      }
 253                  }
 254                  $code_part=chr(rand($a,$b));
 255                  $newcode_length++;
 256                  $newcode = $newcode.$code_part;
 257              }
 258              if ($html && rand(1, 50) == 1) {
 259                  return "<a href=\"http://www.lifetype.net\">$newcode</a>";
 260              }
 261              return $newcode;
 262          }
 263          
 264      }
 265  ?>


Généré le : Mon Nov 26 21:04:15 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics