class FeaturesBundleTest in Features 8.4
Same name and namespace in other branches
- 8.3 tests/src/Unit/FeaturesBundleTest.php \Drupal\Tests\features\Unit\FeaturesBundleTest
@coversDefaultClass Drupal\features\Entity\FeaturesBundle @group features
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\features\Unit\FeaturesBundleTest
Expanded class hierarchy of FeaturesBundleTest
File
- tests/
src/ Unit/ FeaturesBundleTest.php, line 12
Namespace
Drupal\Tests\features\UnitView source
class FeaturesBundleTest extends UnitTestCase {
/**
* {@inheritDoc}
*/
public function setUp() {
parent::setUp();
// Mock an assigner.
$manager = new DummyPluginManager();
// Mock the container.
$container = $this
->prophesize('\\Symfony\\Component\\DependencyInjection\\ContainerInterface');
$container
->get('plugin.manager.features_assignment_method')
->willReturn($manager);
\Drupal::setContainer($container
->reveal());
}
/**
* @covers ::getEnabledAssignments
* @covers ::getAssignmentWeights
* @covers ::getAssignmentSettings
* @covers ::setAssignmentSettings
* @covers ::setAssignmentWeights
* @covers ::setEnabledAssignments
*/
public function testAssignmentSetting() {
// Create an entity.
$settings = [
'foo' => [
'enabled' => TRUE,
'weight' => 0,
'my_setting' => 42,
],
'bar' => [
'enabled' => FALSE,
'weight' => 1,
'another_setting' => 'value',
],
];
$bundle = new FeaturesBundle([
'assignments' => $settings,
], 'features_bundle');
// Get assignments and attributes.
$this
->assertArrayEquals($bundle
->getEnabledAssignments(), [
'foo' => 'foo',
], 'Can get enabled assignments');
$this
->assertArrayEquals($bundle
->getAssignmentWeights(), [
'foo' => 0,
'bar' => 1,
], 'Can get assignment weights');
$this
->assertArrayEquals($bundle
->getAssignmentSettings('foo'), $settings['foo'], 'Can get assignment settings');
$this
->assertArrayEquals($bundle
->getAssignmentSettings(), $settings, 'Can get all assignment settings');
// Change settings.
$settings['foo']['my_setting'] = 97;
$bundle
->setAssignmentSettings('foo', $settings['foo']);
$this
->assertArrayEquals($bundle
->getAssignmentSettings('foo'), $settings['foo'], 'Can change assignment settings');
// Change weights.
$settings['foo']['weight'] = 1;
$settings['bar']['weight'] = 0;
$bundle
->setAssignmentWeights([
'foo' => 1,
'bar' => 0,
]);
$this
->assertArrayEquals($bundle
->getAssignmentWeights(), [
'foo' => 1,
'bar' => 0,
], 'Can change assignment weights');
$this
->assertArrayEquals($bundle
->getAssignmentSettings(), $settings, 'Weight changes are reflected in settings');
// Enable existing assignment.
$settings['bar']['enabled'] = TRUE;
$bundle
->setEnabledAssignments([
'foo',
'bar',
]);
$this
->assertArrayEquals($bundle
->getEnabledAssignments(), [
'foo' => 'foo',
'bar' => 'bar',
], 'Can enable assignment');
$this
->assertArrayEquals($bundle
->getAssignmentSettings(), $settings, 'Enabled assignment status is reflected in settings');
// Disable existing assignments.
$settings['foo']['enabled'] = FALSE;
$settings['bar']['enabled'] = FALSE;
$bundle
->setEnabledAssignments([]);
$this
->assertArrayEquals($bundle
->getEnabledAssignments(), [], 'Can disable assignments');
$this
->assertArrayEquals($bundle
->getAssignmentSettings(), $settings, 'Disabled assignment status is reflected in settings');
// Enable a new assignment.
$settings['foo']['enabled'] = TRUE;
$settings['iggy'] = [
'enabled' => TRUE,
'weight' => 0,
'new_setting' => 3,
];
$bundle
->setEnabledAssignments([
'foo',
'iggy',
]);
$this
->assertArrayEquals($bundle
->getEnabledAssignments(), [
'foo' => 'foo',
'iggy' => 'iggy',
], 'Can enable new assignment');
$bundle
->setAssignmentSettings('iggy', $settings['iggy']);
$this
->assertArrayEquals($bundle
->getAssignmentSettings(), $settings, 'New enabled assignment status is reflected in settings');
}
/**
* @covers ::getFullName
* @covers ::getShortName
* @covers ::SetIsProfile
* @covers ::isProfile
* @covers ::getProfileName
* @covers ::isProfilePackage
* @covers ::inBundle
*/
public function testFullname() {
$bundle = new FeaturesBundle([
'machine_name' => 'mybundle',
'profile_name' => 'mybundle',
], 'mybundle');
$this
->assertFalse($bundle
->isProfile());
// Settings:get('profile_name') isn't defined in test, so this returns NULL.
$this
->assertNull($bundle
->getProfileName());
$this
->assertFalse($bundle
->isProfilePackage('mybundle'));
$this
->assertEquals('mybundle_test', $bundle
->getFullName('test'));
$this
->assertEquals('mybundle_test', $bundle
->getFullName('mybundle_test'));
$this
->assertEquals('mybundle_mybundle', $bundle
->getFullName('mybundle'));
$this
->assertEquals('test', $bundle
->getShortName('test'));
$this
->assertEquals('test', $bundle
->getShortName('mybundle_test'));
$this
->assertEquals('mybundle', $bundle
->getShortName('mybundle_mybundle'));
$this
->assertEquals('mybundle', $bundle
->getShortName('mybundle'));
$this
->assertFalse($bundle
->inBundle('test'));
$this
->assertTrue($bundle
->inBundle('mybundle_test'));
$this
->assertFalse($bundle
->inBundle('mybundle'));
// Now test it as a profile bundle.
$bundle
->setIsProfile(TRUE);
$this
->assertTrue($bundle
->isProfile());
$this
->assertTrue($bundle
->isProfilePackage('mybundle'));
$this
->assertFalse($bundle
->isProfilePackage('standard'));
$this
->assertEquals('mybundle', $bundle
->getProfileName());
$this
->assertEquals('mybundle', $bundle
->getFullName('mybundle'));
$this
->assertFalse($bundle
->inBundle('test'));
$this
->assertTrue($bundle
->inBundle('mybundle_test'));
$this
->assertTrue($bundle
->inBundle('mybundle'));
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
FeaturesBundleTest:: |
public | function |
Overrides UnitTestCase:: |
|
FeaturesBundleTest:: |
public | function | @covers ::getEnabledAssignments @covers ::getAssignmentWeights @covers ::getAssignmentSettings @covers ::setAssignmentSettings @covers ::setAssignmentWeights @covers ::setEnabledAssignments | |
FeaturesBundleTest:: |
public | function | @covers ::getFullName @covers ::getShortName @covers ::SetIsProfile @covers ::isProfile @covers ::getProfileName @covers ::isProfilePackage @covers ::inBundle | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |