Krang::Test::Content - a package to simplify content handling in Krang tests.
use Krang::ClassLoader 'Test::Content';
my $creator = pkg('Test::Content')->new();
# create a Krang::Site object
my $site = $creator->create_site(preview_url => 'preview.fluffydogs.com',
publish_url => 'www.fluffydogs.com',
preview_path => '/tmp/preview_dogs',
publish_path => '/tmp/publish_dogs');
my ($root) = pkg('Category')->find(site_id => $site->site_id);
# create a Krang::Category object.
my $poodle_cat = $creator->create_category(dir => 'poodles',
parent => $root->category_id,
data => 'Fluffy Poodles of the World');
# create another Krang::Category object w/ $poodle_cat as parent.
my $french_poodle_cat = $creator->create_category(dir => 'french',
parent => $poodle_cat,
data => 'French Poodles Uber Alles');
# create a Krang::User user.
my $user = $creator->create_user();
# get the login username if you don't know it
my $login = $user->login();
# get the pw
my $password = $user->password();
# create a Krang::Media object.
my $media = $creator->create_media(category => $poodle_cat);
# create a Krang::Story object under various categories, with media object.
my $story1 = $creator->create_story(category => [$poodle_cat, $french_poodle_cat],
linked_media => [$media]);
# create Story object linking to media and previous story.
my $story2 = $creator->create_story(category => [$poodle_cat, $french_poodle_cat],
linked_story => [$story1],
linked_media => [$media]);
# Where to find the published story on the filesystem
my @paths = $creator->publish_paths(story => $story);
# create a Krang::Contrib object
my $contributor = $creator->create_contrib();
# create a Krang::Template object for root element of story
my $template = $creator->create_template(element => $story->element);
# create a Krang::Template associated with a specific cat
$template = $creator->create_template(element => $story->element, category => $poodle_cat);
# get a Krang::Publisher object
my $publisher = $creator->publisher();
# create and deploy test templates
my @templates = $creator->deploy_test_templates();
# undeploy test templates
$creator->undeploy_test_templates();
# undeploy all live templates
$ok = $creator->undeploy_live_templates();
# restore previously live templates
$ok = $creator->redeploy_live_templates();
# get a random word (returns UTF-8 encoded words if Charset directive is set to utf-8)
my $word = $creator->get_word();
# get a random word exclusively composed of ascii letters a-z and A-Z
my $ascii = $creator->get_word('ascii');
# delete a previously created object
$creator->delete_item(item => $story);
# clean up after the mess you made. Leave things where you found them.
$creator->cleanup();
Krang::Test::Content exists to simplify the process of writing tests for the more advanced subsystems of Krang. Most test suites depend on content existing in the system to test against, and have been rolling their own content-creating routines. This module exists to centralize a lot of that code in one place. Additionally, it provides cleanup functionality, deleting everything that has been created using it.
This module is designed to work with the Default and TestSet1 element sets - it may or may not work with other element sets.
NOTE - It should be clear that this module assumes that the following modules are all in working order: Krang::Site, Krang::Category, Krang::Story, Krang::Media, Krang::Contrib.
$creator = Krang::Test::Content->new()new() will croak with an error if the InstanceElementSet is not Default or TestSet1. At this time, those are the only element sets supported.
$site = $creator->create_site()Arguments:
$category = $creator->create_category()parent. It will croak if unable to create the object.
Arguments:
If parent is not set, it will default to the root category of the first Krang::Site object created by create_site.
$user = $creator->create_user()Accepts the standard arguments passed to Krang::User, or will function with no arguments whatsoever, in which case it will only create the username and password.
NOTE: If a username is specified, create_user will croak if user creation fails because that username is already taken. If no username is specified, create_user will try usernames until it finds one that does not exist.
NOTE2: If group_ids are not specified, create_user will create a user with admin-level access.
$media = $creator->create_media()Arguments:
$story = $creator->create_story()By default, the story will be a single page, with a title, deck, header, and three paragraphs.
Arguments:
If not specified, it will default to 'article', which may or may not work, depending on the element library being used.
If not specified, one will be assigned randomly.
title => 'Confessions of a Poodle Lover'deck => 'Why fluffy dogs make me happy'header => 'In the beginning'This header only applies to the first page. If more than one page is being created in this story, subsuquent pages will have randomly-generated headers.
paragraph => [$p1, $p2, $p3, $p4]This only applies to the first page of a story - additional pages will have three randomly-generated paragraphs each.
@paths = $creator->publish_paths(story => $story)Takes either story or media arguments.
$contributor = $creator->create_contrib()Arguments:
$template = $creator->create_template()The following arguments are taken:
elementcontent is also supplied,
the template constructed will be based on the makeup of $element.
element_namecontent must be supplied as well, as the
template cannot be generated automatically in this case.
contentcategory$category. Otherwise the template will be associated with the root
category for the first Krang::Site object created.
flattenedpredictable$publisher = $creator->publisher()@templates = $creator->deploy_test_templates()This method will check to see if undeploy_live_templates() has been run beforehand.
If not, it will run it first as a safety measure, so as to not clobber live templates, or to cause random errors.
Will croak if there are problems.
Returns a list of Krang::Template templates.
$creator->undeploy_test_templates()@templates = $creator->undeploy_live_templates()Searches the template root for potentially problematic templates, and undeploys them.
$creator->redeploy_live_templates()$word = get_word()$word = get_word('ascii')Depending on the Charset directive in conf/krang.conf those words are utf-8 encoded or not.
$creator->delete_item(item => $krang_object)Any errors thrown by the item itself will not be trapped - they will be passed on to the caller.
If the delete is unsuccessful, it will leave a critical message in the log file and croak.
$creator->cleanup()Will log a critical error message and croak if unsuccessful.
Krang::Test::Content will only work against the ElementSet TestSet1. Any other Element Sets will cause unpredictable behavior.
Flesh out the various create_ methods to support more of the test code in Krang. Ideally, this module would also support bin/krang_floodfill, so that all test suites and dummy data are coming from the same source.