In the previous article, we created the StepObject methods that will be used in our resource protection test. In this one we'll see how to create and use a data file to provide input to those methods. The data files will hold the users and resources we'll need for our test.
This article assumes that you've installed and configured Composer, Codeception, PhpUnit, MODX, Java, WebDriver, and ChromeDriver as described in earlier articles, and that you've created the acceptance test support files from those articles.
Data Files
You'll notice that the createUsers() method only contains user data that is common to all users (along with some fields required by the security/user/create processor pertaining to password and notification methods). The rest of the data comes from the _build/tests/_data/user_data.php file.
In an earlier article, we saw a data file used as a dataProvider for another test. In that case, the test method in the main test file iterated over the members of the array contained in the data file.
Using a dataProvider works in Acceptance tests too, but that's not what we're doing here. This data file is simply raw data for use in the createUsers() and createResources() utility methods in our Objects.php class file. It belongs in the data directory because that's the logical place for raw data. It lets you alter the raw data without rewriting any other code.
Create the user_data.php file manually in the tests/data/ directory, and give it this content (also available at GitHub here):
<?php
$users = array(
array(
'username' => 'PrivateUser',
'email' => 'someUser@gmail.com',
'password' => 'somepassword',
'active' => '1',
'usergroup' => 'PrivateUsers',
'role' => 'TestUser',
),
array(
'username' => 'PublicUser',
'email' => 'someUser@gmail.com',
'password' => 'somepassword',
'active' => '1',
'usergroup' => 'PublicUsers',
'role' => 'TestUser',
),
array(
'username' => 'JoeTester2',
'email' => 'someUser@gmail.com',
'password' => 'TesterPassword',
'active' => '1',
'usergroup' => 'Administrator',
'role' => 'Super User',
),
);
return $users;
We used the same password and email for all users because there's no reason not to. Notice that we've used the name JoeTester2 for the admin username. That's because we created JoeTester earlier and in this test, the users will be removed after the test. If we removed JoeTester, the tests we wrote earlier would fail, since they assume Joe is there. JoeTester should probably be created before running those earlier tests, but we hadn't covered the method of creating Joe in code yet.
The user objects we're creating are quite minimal. The only required fields for users are username, password, and email, we're also setting the active field and, in addition to the user group specified in the data file, the createUsers() method adds each user to the Administrator group with the role of Super User. We do this because they need access to the Manager and MODX has a default ACL entry for the Administrator group giving them that access.
We also need a data file for our resources, so create a resource_data file in the same directory using this code(also available at GitHub here):
<?php
$resources = array(
array(
'pagetitle' => 'PublicResource',
'alias' => 'publicresource',
'content' => 'PublicResourceContent',
'published' => true,
'group' => 'PublicResources',
),
array(
'pagetitle' => 'PrivateResource',
'alias' => 'privateresource',
'content' => 'PrivateResourceContent',
'published' => true,
'group' => 'PrivateResources',
),
);
return $resources;
Coming Up
We have code to create our objects, but before we create our test, we need a new Page object with the CSS and XPath locators used in our test. We'll do that in the next article.
Coming Up
In the next article, we'll create a new page object to hold the locators used in our test. We'll also see some new examples of XPath locators.
For more information on how to use MODX to create a web site, see my web site Bob's Guides, or better yet, buy my book: MODX: The Official Guide.
Looking for high-quality, MODX-friendly hosting? As of May 2016, Bob's Guides is hosted at Hosting.com (formerly A2 Hosting). (More information in the box below.)

Comments (0)
Please login to comment.