View source
<?php
class ViewModePageTestCase extends DrupalWebTestCase {
public $user;
public static function getInfo() {
return array(
'name' => 'Code API Tests',
'description' => 'Unittests for the VMP module',
'group' => 'View Mode Page',
);
}
public function setUp() {
parent::setUp('view_mode_page');
}
public function assertNumberOfPatterns($number) {
$patterns = view_mode_page_get_entity_patterns();
$term = 'patterns';
if ($number == 1) {
$term = 'pattern';
}
$this
->assertEqual(count($patterns), $number, 'There should be ' . $number . ' ' . $term . ', found: ' . count($patterns));
}
public function assertEntityPattern($entity_type, $content_type, $view_mode, $url_pattern, $show_title = FALSE, $title = '') {
$patterns = view_mode_page_get_entity_patterns($entity_type, $content_type, $view_mode);
$this
->assertEqual(count($patterns), 1, 'There should be 1 pattern, found: ' . count($patterns));
if (count($patterns) > 0) {
$this
->assertEqual($patterns[0]->url_pattern, $url_pattern, 'The patterns do not match');
}
}
public function assertPattern($content_type, $view_mode, $url_pattern, $show_title = FALSE, $title = '') {
$this
->assertEntityPattern('node', $content_type, $view_mode, $url_pattern, $show_title, $title);
}
public function testAddEntityPattern() {
$this
->assertNumberOfPatterns(0);
view_mode_page_add_entity_pattern(array(
'entity_type' => 'node',
'content_type' => 'blog_post',
'view_mode' => 'teaser',
'url_pattern' => 'blog/%/teaser',
));
$this
->assertNumberOfPatterns(1);
$this
->assertEntityPattern('node', 'blog_post', 'teaser', 'blog/%/teaser');
}
public function testAddPattern() {
$this
->assertNumberOfPatterns(0);
view_mode_page_add_pattern('blog_post', 'teaser', 'blog/%/teaser');
$this
->assertNumberOfPatterns(1);
$this
->assertPattern('blog_post', 'teaser', 'blog/%/teaser');
}
public function testDeletePattern() {
view_mode_page_add_entity_pattern(array(
'entity_type' => 'node',
'content_type' => 'blog_post',
'view_mode' => 'teaser',
'url_pattern' => 'blog/%/teaser',
));
view_mode_page_add_entity_pattern(array(
'entity_type' => 'node',
'content_type' => 'person',
'view_mode' => 'teaser',
'url_pattern' => 'person/%/teaser',
));
$this
->assertEntityPattern('node', 'blog_post', 'teaser', 'blog/%/teaser');
view_mode_page_delete_patterns('blog_post');
$this
->assertNumberOfPatterns(1);
}
public function testDeleteEntityPattern() {
view_mode_page_add_entity_pattern(array(
'entity_type' => 'node',
'content_type' => 'blog_post',
'view_mode' => 'teaser',
'url_pattern' => 'blog/%/teaser',
));
view_mode_page_add_entity_pattern(array(
'entity_type' => 'node',
'content_type' => 'person',
'view_mode' => 'teaser',
'url_pattern' => 'person/%/teaser',
));
$this
->assertEntityPattern('node', 'blog_post', 'teaser', 'blog/%/teaser');
view_mode_page_delete_entity_patterns('node', 'blog_post');
$this
->assertNumberOfPatterns(1);
}
public function testNoAccessWithoutPermissions() {
$user = $this
->drupalCreateUser(array(
'administer nodes',
'administer content types',
));
$this
->drupalLogin($user);
$this
->drupalGet('admin/structure/types/manage/article/display');
$this
->assertResponse(200, 'User is allowed to access the manage display page');
$this
->assertNoFieldByName('additional_settings[view_mode_page_settings][view_mode_page_url_pattern]');
}
public function testAccessWithPermissions() {
$user = $this
->drupalCreateUser(array(
'administer nodes',
'administer content types',
'administer view mode page',
));
$this
->drupalLogin($user);
$this
->drupalGet('admin/structure/types/manage/article/display');
$this
->assertResponse(200, 'User is allowed to access the manage display page');
$this
->assertFieldByName('additional_settings[view_mode_page_settings][view_mode_page_url_pattern]');
}
public function testArrayReplaceWithNoValues() {
$defaults = array(
'setting' => 123,
'setting2' => 'test',
);
$array = array();
$merged = view_mode_page_array_replace($defaults, $array);
$this
->assertTrue(isset($merged['setting'], $merged['setting2']));
$this
->assertEqual($merged['setting'], 123);
$this
->assertEqual($merged['setting2'], 'test');
}
public function testArrayReplaceWithOverrides() {
$defaults = array(
'setting' => 123,
'setting2' => 'test',
);
$array = array(
'setting' => 456,
);
$merged = view_mode_page_array_replace($defaults, $array);
$this
->assertTrue(isset($merged['setting'], $merged['setting2']));
$this
->assertEqual($merged['setting'], 456);
$this
->assertEqual($merged['setting2'], 'test');
}
}