home | career | drupal | java | mac | mysql | perl | php | scala | uml | unix

Drupal example source code file (image_gallery.test)

This example Drupal source code file (image_gallery.test) is included in the DevDaily.com "Drupal Source Code Warehouse" project. The intent of this project is to help you "Learn Drupal by Example".

PHP - Drupal tags/keywords

array, defaults, edit, foreach, function, galleries, gallery, image, images, images_count, node, php, return, tid

The image_gallery.test Drupal example source code

<?php
// $Id: image_gallery.test,v 1.3 2009/08/08 13:32:53 joachim Exp $

class ImageGalleryTest extends DrupalTestCase {

  protected $web_user;

  function get_info() {
    return array('name' => t('Image Gallery tests'), 'desc' => t('Test Image Gallery module functionality.'), 'group' => 'Image');
  }

  function setUp() {
    parent::setUp();

    // User to create image galleries.
    $this->web_user = $this->drupalCreateUserRolePerm(array('create images', 'view original images', 'edit own images', 'edit any images', 'administer image galleries'));
    $this->drupalGet('logout');
    $this->drupalLoginUser($this->web_user);
  }
  
  function testBasic() {
    $galleries_count = 2;
    $images_count = 5;
    $galleries = array();
    $images = array();
    $this->drupalVariableSet('image_images_per_page', $images_count);
    
    $vid = _image_gallery_get_vid();

    // Create several galleries.
    for ($i = 0; $i < $galleries_count; $i++) {
      $galleries[] = $this->createGallery();
    }
    
    // Create images for galleries.
    $images_misc = file_scan_directory('misc', '.png');
    shuffle($images_misc);
    $images_files = array_slice($images_misc, 0, $images_count);
    for ($i = 0; $i < $images_count; $i++) {
      $tid = $galleries[array_rand($galleries)];
      $edit = array(
        'taxonomy[' . $vid . ']' => $tid,
        'files[image]' => realpath($images_files[$i]->filename),
      );
      $images[] = array(
        'tid' => $tid,
        'node' => $this->createImage($edit),
      );
    }

    // Test view the gallery.
    foreach ($galleries as $tid) {
      $this->drupalGet('image/tid/' . $tid);
      foreach ($images as $image) {
        if ($image['tid'] == $tid) {
          $this->assertWantedRaw($image['node']->title, 'Gallery '. $tid .': image ' . $image['node']->title . ' displayed. %s');
        }
        else {
          $this->assertNoUnwantedRaw($image['node']->title, 'Gallery '. $tid .': image ' . $images['node']->title . ' not displayed. %s');
        }
      }
    }

    // Delete the gallery.
    // This don't work, so use general taxonomy function:
    // $this->drupalPost('admin/content/image/edit/' . $terms[0]->tid, array(), 'Delete');
    foreach ($galleries as $tid) {
      taxonomy_del_term($tid);
    }
  }
  
  function createGallery($edit = array()) {
    $defaults = array(
      'name' => $this->randomName(),
      'parent[0]' => 0,
    );
    $edit = $edit + $defaults;
    $this->drupalPost('admin/content/image/add', $edit, 'Submit');
    $this->assertWantedRaw(t('Created new gallery %term.', array('%term' => $edit['name'])), 'Image gallery created. %s');
    $terms = taxonomy_get_term_by_name($edit['name']);
    return $terms[0]->tid;
  }
  
  function createImage($edit = array()) {
    $defaults = array(
      'title' => $this->randomName(),
      'body' => $this->randomName(),
      'files[image]' => realpath('misc' . DIRECTORY_SEPARATOR . 'druplicon.png'),
    );
    $edit = $edit + $defaults;
    $this->drupalPost('node/add/image', $edit, 'Save');
    $this->assertWantedRaw(t('@type %title has been created.', array('@type' => 'Image', '%title' => $edit['title'])), 'Image created. %s');
    if ($node = node_load(array('title' => $edit['title']))) {
      $this->assertNotNull($node, 'Image found in database. %s');
      return $node;
    }
    return FALSE;
  }
  
}

Other Drupal examples (source code examples)

Here is a short list of links related to this Drupal image_gallery.test source code file:

new blog posts

"Drupal" is a registered trademark of Dries Buytaert.

my drupal tutorials and examples  

Copyright 1998-2016 Alvin Alexander, alvinalexander.com
All Rights Reserved.

Beginning in 2016, a portion of the proceeds from pages under the '/drupal-code-examples/' URI will be donated to charity.