Move into nested docroot

This commit is contained in:
Rob Davies 2017-02-13 15:31:17 +00:00
parent 83a0d3a149
commit c8b70abde9
13405 changed files with 0 additions and 0 deletions

View file

@ -0,0 +1,9 @@
name: 'Forum test views'
type: module
description: 'Provides default views for views forum tests.'
package: Testing
version: VERSION
core: 8.x
dependencies:
- forum
- views

View file

@ -0,0 +1,149 @@
langcode: en
status: true
dependencies: { }
id: test_forum_index
label: test_forum_index
module: views
description: ''
tag: ''
base_table: forum_index
base_field: nid
core: 8.x
display:
default:
display_plugin: default
id: default
display_title: Master
position: null
display_options:
access:
type: none
cache:
type: tag
query:
type: views_query
exposed_form:
type: basic
pager:
type: full
style:
type: default
row:
type: fields
fields:
nid:
table: forum_index
field: nid
id: nid
sticky:
id: sticky
table: forum_index
field: sticky
relationship: none
group_type: group
admin_label: ''
label: Sticky
exclude: false
alter:
alter_text: false
text: ''
make_link: false
path: ''
absolute: false
external: false
replace_spaces: false
path_case: none
trim_whitespace: false
alt: ''
rel: ''
link_class: ''
prefix: ''
suffix: ''
target: ''
nl2br: false
max_length: 0
word_boundary: true
ellipsis: true
more_link: false
more_link_text: ''
more_link_path: ''
strip_tags: false
trim: false
preserve_tags: ''
html: false
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: true
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: true
empty: ''
hide_empty: false
empty_zero: false
hide_alter_empty: true
type: yes-no
type_custom_true: ''
type_custom_false: ''
not: false
plugin_id: boolean
comment_count:
id: comment_count
table: forum_index
field: comment_count
relationship: none
group_type: group
admin_label: ''
label: 'Comment count'
exclude: false
alter:
alter_text: false
text: ''
make_link: false
path: ''
absolute: false
external: false
replace_spaces: false
path_case: none
trim_whitespace: false
alt: ''
rel: ''
link_class: ''
prefix: ''
suffix: ''
target: ''
nl2br: false
max_length: 0
word_boundary: true
ellipsis: true
more_link: false
more_link_text: ''
more_link_path: ''
strip_tags: false
trim: false
preserve_tags: ''
html: false
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: true
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: true
empty: ''
hide_empty: false
empty_zero: false
hide_alter_empty: true
set_precision: false
precision: 0
decimal: .
separator: ','
format_plural: false
format_plural_string: "1\x03@count"
prefix: ''
suffix: ''
plugin_id: numeric
filters: { }
sorts: { }

View file

@ -0,0 +1,65 @@
<?php
namespace Drupal\Tests\forum\Kernel;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\node\Entity\Node;
use Drupal\taxonomy\Entity\Term;
/**
* Tests forum validation constraints.
*
* @group forum
*/
class ForumValidationTest extends EntityKernelTestBase {
/**
* Modules to install.
*
* @var array
*/
public static $modules = ['node', 'options', 'comment', 'taxonomy', 'forum'];
/**
* Tests the forum validation constraints.
*/
public function testValidation() {
// Add a forum.
$forum = Term::create([
'name' => 'forum 1',
'vid' => 'forums',
'forum_container' => 0,
]);
// Add a container.
$container = Term::create([
'name' => 'container 1',
'vid' => 'forums',
'forum_container' => 1,
]);
// Add a forum post.
$forum_post = Node::create([
'type' => 'forum',
'title' => 'Do these pants make my butt look big?',
]);
$violations = $forum_post->validate();
$this->assertEqual(count($violations), 1);
$this->assertEqual($violations[0]->getMessage(), 'This value should not be null.');
// Add the forum term.
$forum_post->set('taxonomy_forums', $forum);
$violations = $forum_post->validate();
$this->assertEqual(count($violations), 0);
// Try to use a container.
$forum_post->set('taxonomy_forums', $container);
$violations = $forum_post->validate();
$this->assertEqual(count($violations), 1);
$this->assertEqual($violations[0]->getMessage(), t('The item %forum is a forum container, not a forum. Select one of the forums below instead.', [
'%forum' => $container->label(),
]));
}
}

View file

@ -0,0 +1,47 @@
<?php
namespace Drupal\Tests\forum\Kernel\Migrate\d6;
use Drupal\config\Tests\SchemaCheckTestTrait;
use Drupal\Tests\migrate_drupal\Kernel\d6\MigrateDrupal6TestBase;
/**
* Upgrade variables to forum.settings.yml.
*
* @group migrate_drupal_6
*/
class MigrateForumConfigsTest extends MigrateDrupal6TestBase {
use SchemaCheckTestTrait;
/**
* {@inheritdoc}
*/
public static $modules = array('comment', 'forum', 'taxonomy');
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->executeMigration('d6_taxonomy_vocabulary');
$this->executeMigration('d6_forum_settings');
}
/**
* Tests migration of forum variables to forum.settings.yml.
*/
public function testForumSettings() {
$config = $this->config('forum.settings');
$this->assertIdentical(15, $config->get('topics.hot_threshold'));
$this->assertIdentical(25, $config->get('topics.page_limit'));
$this->assertIdentical(1, $config->get('topics.order'));
$this->assertIdentical('vocabulary_1_i_0_', $config->get('vocabulary'));
// This is 'forum_block_num_0' in D6, but block:active:limit' in D8.
$this->assertIdentical(5, $config->get('block.active.limit'));
// This is 'forum_block_num_1' in D6, but 'block:new:limit' in D8.
$this->assertIdentical(5, $config->get('block.new.limit'));
$this->assertConfigSchema(\Drupal::service('config.typed'), 'forum.settings', $config->get());
}
}

View file

@ -0,0 +1,47 @@
<?php
namespace Drupal\Tests\forum\Kernel\Migrate\d7;
use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;
/**
* Tests migration of Forum's variables to configuration.
*
* @group forum
*/
class MigrateForumSettingsTest extends MigrateDrupal7TestBase {
// Don't alphabetize these. They're in dependency order.
public static $modules = [
'comment',
'field',
'filter',
'text',
'node',
'taxonomy',
'forum',
];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->executeMigration('d7_taxonomy_vocabulary');
$this->executeMigration('d7_forum_settings');
}
/**
* Tests the migration of Forum's settings to configuration.
*/
public function testForumSettingsMigration() {
$config = $this->config('forum.settings');
$this->assertIdentical(9, $config->get('block.active.limit'));
$this->assertIdentical(4, $config->get('block.new.limit'));
$this->assertIdentical(10, $config->get('topics.hot_threshold'));
$this->assertIdentical(25, $config->get('topics.page_limit'));
$this->assertIdentical(1, $config->get('topics.order'));
$this->assertIdentical('forums', $config->get('vocabulary'));
}
}

View file

@ -0,0 +1,160 @@
<?php
namespace Drupal\Tests\forum\Unit\Breadcrumb;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Link;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\DependencyInjection\Container;
/**
* @coversDefaultClass \Drupal\forum\Breadcrumb\ForumBreadcrumbBuilderBase
* @group forum
*/
class ForumBreadcrumbBuilderBaseTest extends UnitTestCase {
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$cache_contexts_manager = $this->getMockBuilder('Drupal\Core\Cache\Context\CacheContextsManager')
->disableOriginalConstructor()
->getMock();
$cache_contexts_manager->method('assertValidTokens')->willReturn(TRUE);
$container = new Container();
$container->set('cache_contexts_manager', $cache_contexts_manager);
\Drupal::setContainer($container);
}
/**
* Tests ForumBreadcrumbBuilderBase::__construct().
*
* @covers ::__construct
*/
public function testConstructor() {
// Make some test doubles.
$entity_manager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
$config_factory = $this->getConfigFactoryStub(
array(
'forum.settings' => array('IAmATestKey' => 'IAmATestValue'),
)
);
$forum_manager = $this->getMock('Drupal\forum\ForumManagerInterface');
$translation_manager = $this->getMock('Drupal\Core\StringTranslation\TranslationInterface');
// Make an object to test.
$builder = $this->getMockForAbstractClass(
'Drupal\forum\Breadcrumb\ForumBreadcrumbBuilderBase',
// Constructor array.
array(
$entity_manager,
$config_factory,
$forum_manager,
$translation_manager,
)
);
// Reflect upon our properties, except for config which is a special case.
$property_names = array(
'entityManager' => $entity_manager,
'forumManager' => $forum_manager,
'stringTranslation' => $translation_manager,
);
foreach ($property_names as $property_name => $property_value) {
$this->assertAttributeEquals(
$property_value, $property_name, $builder
);
}
// Test that the constructor made a config object with our info in it.
$reflector = new \ReflectionClass($builder);
$ref_property = $reflector->getProperty('config');
$ref_property->setAccessible(TRUE);
$config = $ref_property->getValue($builder);
$this->assertEquals('IAmATestValue', $config->get('IAmATestKey'));
}
/**
* Tests ForumBreadcrumbBuilderBase::build().
*
* @see \Drupal\forum\Breadcrumb\ForumBreadcrumbBuilderBase::build()
*
* @covers ::build
*/
public function testBuild() {
// Build all our dependencies, backwards.
$translation_manager = $this->getMockBuilder('Drupal\Core\StringTranslation\TranslationInterface')
->disableOriginalConstructor()
->getMock();
$forum_manager = $this->getMockBuilder('Drupal\forum\ForumManagerInterface')
->disableOriginalConstructor()
->getMock();
$prophecy = $this->prophesize('Drupal\taxonomy\VocabularyInterface');
$prophecy->label()->willReturn('Fora_is_the_plural_of_forum');
$prophecy->id()->willReturn(5);
$prophecy->getCacheTags()->willReturn(['taxonomy_vocabulary:5']);
$prophecy->getCacheContexts()->willReturn([]);
$prophecy->getCacheMaxAge()->willReturn(Cache::PERMANENT);
$vocab_storage = $this->getMock('Drupal\Core\Entity\EntityStorageInterface');
$vocab_storage->expects($this->any())
->method('load')
->will($this->returnValueMap(array(
array('forums', $prophecy->reveal()),
)));
$entity_manager = $this->getMockBuilder('Drupal\Core\Entity\EntityManagerInterface')
->disableOriginalConstructor()
->getMock();
$entity_manager->expects($this->any())
->method('getStorage')
->will($this->returnValueMap(array(
array('taxonomy_vocabulary', $vocab_storage),
)));
$config_factory = $this->getConfigFactoryStub(
array(
'forum.settings' => array(
'vocabulary' => 'forums',
),
)
);
// Build a breadcrumb builder to test.
$breadcrumb_builder = $this->getMockForAbstractClass(
'Drupal\forum\Breadcrumb\ForumBreadcrumbBuilderBase',
// Constructor array.
array(
$entity_manager,
$config_factory,
$forum_manager,
$translation_manager,
)
);
// Add a translation manager for t().
$translation_manager = $this->getStringTranslationStub();
$breadcrumb_builder->setStringTranslation($translation_manager);
// Our empty data set.
$route_match = $this->getMock('Drupal\Core\Routing\RouteMatchInterface');
// Expected result set.
$expected = array(
Link::createFromRoute('Home', '<front>'),
Link::createFromRoute('Fora_is_the_plural_of_forum', 'forum.index'),
);
// And finally, the test.
$breadcrumb = $breadcrumb_builder->build($route_match);
$this->assertEquals($expected, $breadcrumb->getLinks());
$this->assertEquals(['route'], $breadcrumb->getCacheContexts());
$this->assertEquals(['taxonomy_vocabulary:5'], $breadcrumb->getCacheTags());
$this->assertEquals(Cache::PERMANENT, $breadcrumb->getCacheMaxAge());
}
}

View file

@ -0,0 +1,237 @@
<?php
namespace Drupal\Tests\forum\Unit\Breadcrumb;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Link;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\DependencyInjection\Container;
/**
* @coversDefaultClass \Drupal\forum\Breadcrumb\ForumListingBreadcrumbBuilder
* @group forum
*/
class ForumListingBreadcrumbBuilderTest extends UnitTestCase {
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$cache_contexts_manager = $this->getMockBuilder('Drupal\Core\Cache\Context\CacheContextsManager')
->disableOriginalConstructor()
->getMock();
$cache_contexts_manager->method('assertValidTokens')->willReturn(TRUE);
$container = new Container();
$container->set('cache_contexts_manager', $cache_contexts_manager);
\Drupal::setContainer($container);
}
/**
* Tests ForumListingBreadcrumbBuilder::applies().
*
* @param bool $expected
* ForumListingBreadcrumbBuilder::applies() expected result.
* @param string|null $route_name
* (optional) A route name.
* @param array $parameter_map
* (optional) An array of parameter names and values.
*
* @dataProvider providerTestApplies
* @covers ::applies
*/
public function testApplies($expected, $route_name = NULL, $parameter_map = array()) {
// Make some test doubles.
$entity_manager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
$config_factory = $this->getConfigFactoryStub(array());
$forum_manager = $this->getMock('Drupal\forum\ForumManagerInterface');
$translation_manager = $this->getMock('Drupal\Core\StringTranslation\TranslationInterface');
// Make an object to test.
$builder = $this->getMockBuilder('Drupal\forum\Breadcrumb\ForumListingBreadcrumbBuilder')
->setConstructorArgs(array(
$entity_manager,
$config_factory,
$forum_manager,
$translation_manager,
))
->setMethods(NULL)
->getMock();
$route_match = $this->getMock('Drupal\Core\Routing\RouteMatchInterface');
$route_match->expects($this->once())
->method('getRouteName')
->will($this->returnValue($route_name));
$route_match->expects($this->any())
->method('getParameter')
->will($this->returnValueMap($parameter_map));
$this->assertEquals($expected, $builder->applies($route_match));
}
/**
* Provides test data for testApplies().
*
* @return array
* Array of datasets for testApplies(). Structured as such:
* - ForumListBreadcrumbBuilder::applies() expected result.
* - ForumListBreadcrumbBuilder::applies() $attributes input array.
*/
public function providerTestApplies() {
// Send a Node mock, because NodeInterface cannot be mocked.
$mock_term = $this->getMockBuilder('Drupal\taxonomy\Entity\Term')
->disableOriginalConstructor()
->getMock();
return array(
array(
FALSE,
),
array(
FALSE,
'NOT.forum.page',
),
array(
FALSE,
'forum.page',
),
array(
TRUE,
'forum.page',
array(array('taxonomy_term', 'anything')),
),
array(
TRUE,
'forum.page',
array(array('taxonomy_term', $mock_term)),
),
);
}
/**
* Tests ForumListingBreadcrumbBuilder::build().
*
* @see \Drupal\forum\ForumListingBreadcrumbBuilder::build()
*
* @covers ::build
*/
public function testBuild() {
// Build all our dependencies, backwards.
$translation_manager = $this->getMockBuilder('Drupal\Core\StringTranslation\TranslationInterface')
->disableOriginalConstructor()
->getMock();
$prophecy = $this->prophesize('Drupal\taxonomy\Entity\Term');
$prophecy->label()->willReturn('Something');
$prophecy->id()->willReturn(1);
$prophecy->getCacheTags()->willReturn(['taxonomy_term:1']);
$prophecy->getCacheContexts()->willReturn([]);
$prophecy->getCacheMaxAge()->willReturn(Cache::PERMANENT);
$term1 = $prophecy->reveal();
$prophecy = $this->prophesize('Drupal\taxonomy\Entity\Term');
$prophecy->label()->willReturn('Something else');
$prophecy->id()->willReturn(2);
$prophecy->getCacheTags()->willReturn(['taxonomy_term:2']);
$prophecy->getCacheContexts()->willReturn([]);
$prophecy->getCacheMaxAge()->willReturn(Cache::PERMANENT);
$term2 = $prophecy->reveal();
$forum_manager = $this->getMock('Drupal\forum\ForumManagerInterface');
$forum_manager->expects($this->at(0))
->method('getParents')
->will($this->returnValue(array($term1)));
$forum_manager->expects($this->at(1))
->method('getParents')
->will($this->returnValue(array($term1, $term2)));
// The root forum.
$prophecy = $this->prophesize('Drupal\taxonomy\VocabularyInterface');
$prophecy->label()->willReturn('Fora_is_the_plural_of_forum');
$prophecy->id()->willReturn(5);
$prophecy->getCacheTags()->willReturn(['taxonomy_vocabulary:5']);
$prophecy->getCacheContexts()->willReturn([]);
$prophecy->getCacheMaxAge()->willReturn(Cache::PERMANENT);
$vocab_storage = $this->getMock('Drupal\Core\Entity\EntityStorageInterface');
$vocab_storage->expects($this->any())
->method('load')
->will($this->returnValueMap(array(
array('forums', $prophecy->reveal()),
)));
$entity_manager = $this->getMockBuilder('Drupal\Core\Entity\EntityManagerInterface')
->disableOriginalConstructor()
->getMock();
$entity_manager->expects($this->any())
->method('getStorage')
->will($this->returnValueMap(array(
array('taxonomy_vocabulary', $vocab_storage),
)));
$config_factory = $this->getConfigFactoryStub(
array(
'forum.settings' => array(
'vocabulary' => 'forums',
),
)
);
// Build a breadcrumb builder to test.
$breadcrumb_builder = $this->getMock(
'Drupal\forum\Breadcrumb\ForumListingBreadcrumbBuilder', NULL, array(
$entity_manager,
$config_factory,
$forum_manager,
$translation_manager,
)
);
// Add a translation manager for t().
$translation_manager = $this->getStringTranslationStub();
$breadcrumb_builder->setStringTranslation($translation_manager);
// The forum listing we need a breadcrumb back from.
$prophecy = $this->prophesize('Drupal\taxonomy\Entity\Term');
$prophecy->label()->willReturn('You_should_not_see_this');
$prophecy->id()->willReturn(23);
$prophecy->getCacheTags()->willReturn(['taxonomy_term:23']);
$prophecy->getCacheContexts()->willReturn([]);
$prophecy->getCacheMaxAge()->willReturn(Cache::PERMANENT);
$forum_listing = $prophecy->reveal();
// Our data set.
$route_match = $this->getMock('Drupal\Core\Routing\RouteMatchInterface');
$route_match->expects($this->exactly(2))
->method('getParameter')
->with('taxonomy_term')
->will($this->returnValue($forum_listing));
// First test.
$expected1 = array(
Link::createFromRoute('Home', '<front>'),
Link::createFromRoute('Fora_is_the_plural_of_forum', 'forum.index'),
Link::createFromRoute('Something', 'forum.page', array('taxonomy_term' => 1)),
);
$breadcrumb = $breadcrumb_builder->build($route_match);
$this->assertEquals($expected1, $breadcrumb->getLinks());
$this->assertEquals(['route'], $breadcrumb->getCacheContexts());
$this->assertEquals(['taxonomy_term:1', 'taxonomy_term:23', 'taxonomy_vocabulary:5'], $breadcrumb->getCacheTags());
$this->assertEquals(Cache::PERMANENT, $breadcrumb->getCacheMaxAge());
// Second test.
$expected2 = array(
Link::createFromRoute('Home', '<front>'),
Link::createFromRoute('Fora_is_the_plural_of_forum', 'forum.index'),
Link::createFromRoute('Something else', 'forum.page', array('taxonomy_term' => 2)),
Link::createFromRoute('Something', 'forum.page', array('taxonomy_term' => 1)),
);
$breadcrumb = $breadcrumb_builder->build($route_match);
$this->assertEquals($expected2, $breadcrumb->getLinks());
$this->assertEquals(['route'], $breadcrumb->getCacheContexts());
$this->assertEquals(['taxonomy_term:1', 'taxonomy_term:2', 'taxonomy_term:23', 'taxonomy_vocabulary:5'], $breadcrumb->getCacheTags());
$this->assertEquals(Cache::PERMANENT, $breadcrumb->getCacheMaxAge());
}
}

View file

@ -0,0 +1,243 @@
<?php
namespace Drupal\Tests\forum\Unit\Breadcrumb;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Link;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\DependencyInjection\Container;
/**
* @coversDefaultClass \Drupal\forum\Breadcrumb\ForumNodeBreadcrumbBuilder
* @group forum
*/
class ForumNodeBreadcrumbBuilderTest extends UnitTestCase {
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$cache_contexts_manager = $this->getMockBuilder('Drupal\Core\Cache\Context\CacheContextsManager')
->disableOriginalConstructor()
->getMock();
$cache_contexts_manager->method('assertValidTokens')->willReturn(TRUE);
$container = new Container();
$container->set('cache_contexts_manager', $cache_contexts_manager);
\Drupal::setContainer($container);
}
/**
* Tests ForumNodeBreadcrumbBuilder::applies().
*
* @param bool $expected
* ForumNodeBreadcrumbBuilder::applies() expected result.
* @param string|null $route_name
* (optional) A route name.
* @param array $parameter_map
* (optional) An array of parameter names and values.
*
* @dataProvider providerTestApplies
* @covers ::applies
*/
public function testApplies($expected, $route_name = NULL, $parameter_map = array()) {
// Make some test doubles.
$entity_manager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
$config_factory = $this->getConfigFactoryStub(array());
$forum_manager = $this->getMock('Drupal\forum\ForumManagerInterface');
$forum_manager->expects($this->any())
->method('checkNodeType')
->will($this->returnValue(TRUE));
$translation_manager = $this->getMock('Drupal\Core\StringTranslation\TranslationInterface');
// Make an object to test.
$builder = $this->getMockBuilder('Drupal\forum\Breadcrumb\ForumNodeBreadcrumbBuilder')
->setConstructorArgs(
array(
$entity_manager,
$config_factory,
$forum_manager,
$translation_manager,
)
)
->setMethods(NULL)
->getMock();
$route_match = $this->getMock('Drupal\Core\Routing\RouteMatchInterface');
$route_match->expects($this->once())
->method('getRouteName')
->will($this->returnValue($route_name));
$route_match->expects($this->any())
->method('getParameter')
->will($this->returnValueMap($parameter_map));
$this->assertEquals($expected, $builder->applies($route_match));
}
/**
* Provides test data for testApplies().
*
* Note that this test is incomplete, because we can't mock NodeInterface.
*
* @return array
* Array of datasets for testApplies(). Structured as such:
* - ForumNodeBreadcrumbBuilder::applies() expected result.
* - ForumNodeBreadcrumbBuilder::applies() $attributes input array.
*/
public function providerTestApplies() {
// Send a Node mock, because NodeInterface cannot be mocked.
$mock_node = $this->getMockBuilder('Drupal\node\Entity\Node')
->disableOriginalConstructor()
->getMock();
return array(
array(
FALSE,
),
array(
FALSE,
'NOT.entity.node.canonical',
),
array(
FALSE,
'entity.node.canonical',
),
array(
FALSE,
'entity.node.canonical',
array(array('node', NULL)),
),
array(
TRUE,
'entity.node.canonical',
array(array('node', $mock_node)),
),
);
}
/**
* Tests ForumNodeBreadcrumbBuilder::build().
*
* @see \Drupal\forum\ForumNodeBreadcrumbBuilder::build()
* @covers ::build
*/
public function testBuild() {
// Build all our dependencies, backwards.
$translation_manager = $this->getMockBuilder('Drupal\Core\StringTranslation\TranslationInterface')
->disableOriginalConstructor()
->getMock();
$prophecy = $this->prophesize('Drupal\taxonomy\Entity\Term');
$prophecy->label()->willReturn('Something');
$prophecy->id()->willReturn(1);
$prophecy->getCacheTags()->willReturn(['taxonomy_term:1']);
$prophecy->getCacheContexts()->willReturn([]);
$prophecy->getCacheMaxAge()->willReturn(Cache::PERMANENT);
$term1 = $prophecy->reveal();
$prophecy = $this->prophesize('Drupal\taxonomy\Entity\Term');
$prophecy->label()->willReturn('Something else');
$prophecy->id()->willReturn(2);
$prophecy->getCacheTags()->willReturn(['taxonomy_term:2']);
$prophecy->getCacheContexts()->willReturn([]);
$prophecy->getCacheMaxAge()->willReturn(Cache::PERMANENT);
$term2 = $prophecy->reveal();
$forum_manager = $this->getMockBuilder('Drupal\forum\ForumManagerInterface')
->disableOriginalConstructor()
->getMock();
$forum_manager->expects($this->at(0))
->method('getParents')
->will($this->returnValue(array($term1)));
$forum_manager->expects($this->at(1))
->method('getParents')
->will($this->returnValue(array($term1, $term2)));
$prophecy = $this->prophesize('Drupal\taxonomy\VocabularyInterface');
$prophecy->label()->willReturn('Forums');
$prophecy->id()->willReturn(5);
$prophecy->getCacheTags()->willReturn(['taxonomy_vocabulary:5']);
$prophecy->getCacheContexts()->willReturn([]);
$prophecy->getCacheMaxAge()->willReturn(Cache::PERMANENT);
$vocab_storage = $this->getMock('Drupal\Core\Entity\EntityStorageInterface');
$vocab_storage->expects($this->any())
->method('load')
->will($this->returnValueMap(array(
array('forums', $prophecy->reveal()),
)));
$entity_manager = $this->getMockBuilder('Drupal\Core\Entity\EntityManagerInterface')
->disableOriginalConstructor()
->getMock();
$entity_manager->expects($this->any())
->method('getStorage')
->will($this->returnValueMap(array(
array('taxonomy_vocabulary', $vocab_storage),
)));
$config_factory = $this->getConfigFactoryStub(
array(
'forum.settings' => array(
'vocabulary' => 'forums',
),
)
);
// Build a breadcrumb builder to test.
$breadcrumb_builder = $this->getMock(
'Drupal\forum\Breadcrumb\ForumNodeBreadcrumbBuilder', NULL, array(
$entity_manager,
$config_factory,
$forum_manager,
$translation_manager,
)
);
// Add a translation manager for t().
$translation_manager = $this->getStringTranslationStub();
$property = new \ReflectionProperty('Drupal\forum\Breadcrumb\ForumNodeBreadcrumbBuilder', 'stringTranslation');
$property->setAccessible(TRUE);
$property->setValue($breadcrumb_builder, $translation_manager);
// The forum node we need a breadcrumb back from.
$forum_node = $this->getMockBuilder('Drupal\node\Entity\Node')
->disableOriginalConstructor()
->getMock();
// Our data set.
$route_match = $this->getMock('Drupal\Core\Routing\RouteMatchInterface');
$route_match->expects($this->exactly(2))
->method('getParameter')
->with('node')
->will($this->returnValue($forum_node));
// First test.
$expected1 = array(
Link::createFromRoute('Home', '<front>'),
Link::createFromRoute('Forums', 'forum.index'),
Link::createFromRoute('Something', 'forum.page', array('taxonomy_term' => 1)),
);
$breadcrumb = $breadcrumb_builder->build($route_match);
$this->assertEquals($expected1, $breadcrumb->getLinks());
$this->assertEquals(['route'], $breadcrumb->getCacheContexts());
$this->assertEquals(['taxonomy_term:1', 'taxonomy_vocabulary:5'], $breadcrumb->getCacheTags());
$this->assertEquals(Cache::PERMANENT, $breadcrumb->getCacheMaxAge());
// Second test.
$expected2 = array(
Link::createFromRoute('Home', '<front>'),
Link::createFromRoute('Forums', 'forum.index'),
Link::createFromRoute('Something else', 'forum.page', array('taxonomy_term' => 2)),
Link::createFromRoute('Something', 'forum.page', array('taxonomy_term' => 1)),
);
$breadcrumb = $breadcrumb_builder->build($route_match);
$this->assertEquals($expected2, $breadcrumb->getLinks());
$this->assertEquals(['route'], $breadcrumb->getCacheContexts());
$this->assertEquals(['taxonomy_term:1', 'taxonomy_term:2', 'taxonomy_vocabulary:5'], $breadcrumb->getCacheTags());
$this->assertEquals(Cache::PERMANENT, $breadcrumb->getCacheMaxAge());
}
}

View file

@ -0,0 +1,83 @@
<?php
namespace Drupal\Tests\forum\Unit;
use Drupal\Tests\UnitTestCase;
/**
* @coversDefaultClass \Drupal\forum\ForumManager
* @group forum
*/
class ForumManagerTest extends UnitTestCase {
/**
* Tests ForumManager::getIndex().
*/
public function testGetIndex() {
$entity_manager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
$storage = $this->getMockBuilder('\Drupal\taxonomy\VocabularyStorage')
->disableOriginalConstructor()
->getMock();
$config_factory = $this->getMock('\Drupal\Core\Config\ConfigFactoryInterface');
$config = $this->getMockBuilder('\Drupal\Core\Config\Config')
->disableOriginalConstructor()
->getMock();
$config_factory->expects($this->once())
->method('get')
->will($this->returnValue($config));
$config->expects($this->once())
->method('get')
->will($this->returnValue('forums'));
$entity_manager->expects($this->once())
->method('getStorage')
->will($this->returnValue($storage));
// This is sufficient for testing purposes.
$term = new \stdClass();
$storage->expects($this->once())
->method('create')
->will($this->returnValue($term));
$connection = $this->getMockBuilder('\Drupal\Core\Database\Connection')
->disableOriginalConstructor()
->getMock();
$translation_manager = $this->getMockBuilder('\Drupal\Core\StringTranslation\TranslationManager')
->disableOriginalConstructor()
->getMock();
$comment_manager = $this->getMockBuilder('\Drupal\comment\CommentManagerInterface')
->disableOriginalConstructor()
->getMock();
$manager = $this->getMock('\Drupal\forum\ForumManager', array('getChildren'), array(
$config_factory,
$entity_manager,
$connection,
$translation_manager,
$comment_manager,
));
$manager->expects($this->once())
->method('getChildren')
->will($this->returnValue(array()));
// Get the index once.
$index1 = $manager->getIndex();
// Get it again. This should not return the previously generated index. If
// it does not, then the test will fail as the mocked methods will be called
// more than once.
$index2 = $manager->getIndex();
$this->assertEquals($index1, $index2);
}
}

View file

@ -0,0 +1,234 @@
<?php
namespace Drupal\Tests\forum\Unit;
use Drupal\simpletest\AssertHelperTrait;
use Drupal\Tests\UnitTestCase;
/**
* @coversDefaultClass \Drupal\forum\ForumUninstallValidator
* @group forum
*/
class ForumUninstallValidatorTest extends UnitTestCase {
use AssertHelperTrait;
/**
* @var \Drupal\forum\ForumUninstallValidator|\PHPUnit_Framework_MockObject_MockObject
*/
protected $forumUninstallValidator;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->forumUninstallValidator = $this->getMockBuilder('Drupal\forum\ForumUninstallValidator')
->disableOriginalConstructor()
->setMethods(['hasForumNodes', 'hasTermsForVocabulary', 'getForumVocabulary'])
->getMock();
$this->forumUninstallValidator->setStringTranslation($this->getStringTranslationStub());
}
/**
* @covers ::validate
*/
public function testValidateNotForum() {
$this->forumUninstallValidator->expects($this->never())
->method('hasForumNodes');
$this->forumUninstallValidator->expects($this->never())
->method('hasTermsForVocabulary');
$this->forumUninstallValidator->expects($this->never())
->method('getForumVocabulary');
$module = 'not_forum';
$expected = [];
$reasons = $this->forumUninstallValidator->validate($module);
$this->assertSame($expected, $this->castSafeStrings($reasons));
}
/**
* @covers ::validate
*/
public function testValidate() {
$this->forumUninstallValidator->expects($this->once())
->method('hasForumNodes')
->willReturn(FALSE);
$vocabulary = $this->getMock('Drupal\taxonomy\VocabularyInterface');
$this->forumUninstallValidator->expects($this->once())
->method('getForumVocabulary')
->willReturn($vocabulary);
$this->forumUninstallValidator->expects($this->once())
->method('hasTermsForVocabulary')
->willReturn(FALSE);
$module = 'forum';
$expected = [];
$reasons = $this->forumUninstallValidator->validate($module);
$this->assertSame($expected, $this->castSafeStrings($reasons));
}
/**
* @covers ::validate
*/
public function testValidateHasForumNodes() {
$this->forumUninstallValidator->expects($this->once())
->method('hasForumNodes')
->willReturn(TRUE);
$vocabulary = $this->getMock('Drupal\taxonomy\VocabularyInterface');
$this->forumUninstallValidator->expects($this->once())
->method('getForumVocabulary')
->willReturn($vocabulary);
$this->forumUninstallValidator->expects($this->once())
->method('hasTermsForVocabulary')
->willReturn(FALSE);
$module = 'forum';
$expected = [
'To uninstall Forum, first delete all <em>Forum</em> content',
];
$reasons = $this->forumUninstallValidator->validate($module);
$this->assertSame($expected, $this->castSafeStrings($reasons));
}
/**
* @covers ::validate
*/
public function testValidateHasTermsForVocabularyWithNodesAccess() {
$this->forumUninstallValidator->expects($this->once())
->method('hasForumNodes')
->willReturn(TRUE);
$vocabulary = $this->getMock('Drupal\taxonomy\VocabularyInterface');
$vocabulary->expects($this->once())
->method('label')
->willReturn('Vocabulary label');
$vocabulary->expects($this->once())
->method('url')
->willReturn('/path/to/vocabulary/overview');
$vocabulary->expects($this->once())
->method('access')
->willReturn(TRUE);
$this->forumUninstallValidator->expects($this->once())
->method('getForumVocabulary')
->willReturn($vocabulary);
$this->forumUninstallValidator->expects($this->once())
->method('hasTermsForVocabulary')
->willReturn(TRUE);
$module = 'forum';
$expected = [
'To uninstall Forum, first delete all <em>Forum</em> content',
'To uninstall Forum, first delete all <a href="/path/to/vocabulary/overview"><em class="placeholder">Vocabulary label</em></a> terms',
];
$reasons = $this->forumUninstallValidator->validate($module);
$this->assertSame($expected, $this->castSafeStrings($reasons));
}
/**
* @covers ::validate
*/
public function testValidateHasTermsForVocabularyWithNodesNoAccess() {
$this->forumUninstallValidator->expects($this->once())
->method('hasForumNodes')
->willReturn(TRUE);
$vocabulary = $this->getMock('Drupal\taxonomy\VocabularyInterface');
$vocabulary->expects($this->once())
->method('label')
->willReturn('Vocabulary label');
$vocabulary->expects($this->never())
->method('url');
$vocabulary->expects($this->once())
->method('access')
->willReturn(FALSE);
$this->forumUninstallValidator->expects($this->once())
->method('getForumVocabulary')
->willReturn($vocabulary);
$this->forumUninstallValidator->expects($this->once())
->method('hasTermsForVocabulary')
->willReturn(TRUE);
$module = 'forum';
$expected = [
'To uninstall Forum, first delete all <em>Forum</em> content',
'To uninstall Forum, first delete all <em class="placeholder">Vocabulary label</em> terms',
];
$reasons = $this->forumUninstallValidator->validate($module);
$this->assertSame($expected, $this->castSafeStrings($reasons));
}
/**
* @covers ::validate
*/
public function testValidateHasTermsForVocabularyAccess() {
$this->forumUninstallValidator->expects($this->once())
->method('hasForumNodes')
->willReturn(FALSE);
$vocabulary = $this->getMock('Drupal\taxonomy\VocabularyInterface');
$vocabulary->expects($this->once())
->method('url')
->willReturn('/path/to/vocabulary/overview');
$vocabulary->expects($this->once())
->method('label')
->willReturn('Vocabulary label');
$vocabulary->expects($this->once())
->method('access')
->willReturn(TRUE);
$this->forumUninstallValidator->expects($this->once())
->method('getForumVocabulary')
->willReturn($vocabulary);
$this->forumUninstallValidator->expects($this->once())
->method('hasTermsForVocabulary')
->willReturn(TRUE);
$module = 'forum';
$expected = [
'To uninstall Forum, first delete all <a href="/path/to/vocabulary/overview"><em class="placeholder">Vocabulary label</em></a> terms',
];
$reasons = $this->forumUninstallValidator->validate($module);
$this->assertSame($expected, $this->castSafeStrings($reasons));
}
/**
* @covers ::validate
*/
public function testValidateHasTermsForVocabularyNoAccess() {
$this->forumUninstallValidator->expects($this->once())
->method('hasForumNodes')
->willReturn(FALSE);
$vocabulary = $this->getMock('Drupal\taxonomy\VocabularyInterface');
$vocabulary->expects($this->once())
->method('label')
->willReturn('Vocabulary label');
$vocabulary->expects($this->never())
->method('url');
$vocabulary->expects($this->once())
->method('access')
->willReturn(FALSE);
$this->forumUninstallValidator->expects($this->once())
->method('getForumVocabulary')
->willReturn($vocabulary);
$this->forumUninstallValidator->expects($this->once())
->method('hasTermsForVocabulary')
->willReturn(TRUE);
$module = 'forum';
$expected = [
'To uninstall Forum, first delete all <em class="placeholder">Vocabulary label</em> terms',
];
$reasons = $this->forumUninstallValidator->validate($module);
$this->assertSame($expected, $this->castSafeStrings($reasons));
}
}