Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
D
drupal
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Custom Issue Tracker
Custom Issue Tracker
Labels
Merge Requests
294
Merge Requests
294
Requirements
Requirements
List
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Analytics
Analytics
Code Review
Insights
Issue
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
project
drupal
Commits
282f6b90
Commit
282f6b90
authored
Mar 09, 2013
by
webchick
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue
#1932382
by Berdir, swentel: Use DrupalUnitTestBase for Field API tests.
parent
80fd0f97
Changes
22
Hide whitespace changes
Inline
Side-by-side
Showing
22 changed files
with
479 additions
and
270 deletions
+479
-270
core/modules/email/lib/Drupal/email/Tests/EmailItemTest.php
core/modules/email/lib/Drupal/email/Tests/EmailItemTest.php
+2
-2
core/modules/field/lib/Drupal/field/Tests/ActiveTest.php
core/modules/field/lib/Drupal/field/Tests/ActiveTest.php
+89
-0
core/modules/field/lib/Drupal/field/Tests/BulkDeleteTest.php
core/modules/field/lib/Drupal/field/Tests/BulkDeleteTest.php
+1
-8
core/modules/field/lib/Drupal/field/Tests/CrudTest.php
core/modules/field/lib/Drupal/field/Tests/CrudTest.php
+2
-65
core/modules/field/lib/Drupal/field/Tests/DisplayApiTest.php
core/modules/field/lib/Drupal/field/Tests/DisplayApiTest.php
+11
-18
core/modules/field/lib/Drupal/field/Tests/FieldAttachOtherTest.php
...les/field/lib/Drupal/field/Tests/FieldAttachOtherTest.php
+6
-1
core/modules/field/lib/Drupal/field/Tests/FieldAttachStorageTest.php
...s/field/lib/Drupal/field/Tests/FieldAttachStorageTest.php
+6
-1
core/modules/field/lib/Drupal/field/Tests/FieldAttachTestBase.php
...ules/field/lib/Drupal/field/Tests/FieldAttachTestBase.php
+0
-62
core/modules/field/lib/Drupal/field/Tests/FieldInfoTest.php
core/modules/field/lib/Drupal/field/Tests/FieldInfoTest.php
+1
-8
core/modules/field/lib/Drupal/field/Tests/FieldInstanceCrudTest.php
...es/field/lib/Drupal/field/Tests/FieldInstanceCrudTest.php
+1
-8
core/modules/field/lib/Drupal/field/Tests/FieldItemUnitTestBase.php
...es/field/lib/Drupal/field/Tests/FieldItemUnitTestBase.php
+0
-32
core/modules/field/lib/Drupal/field/Tests/FieldUnitTestBase.php
...odules/field/lib/Drupal/field/Tests/FieldUnitTestBase.php
+228
-0
core/modules/field/lib/Drupal/field/Tests/ShapeItemTest.php
core/modules/field/lib/Drupal/field/Tests/ShapeItemTest.php
+1
-1
core/modules/field/lib/Drupal/field/Tests/TestItemTest.php
core/modules/field/lib/Drupal/field/Tests/TestItemTest.php
+1
-1
core/modules/field/lib/Drupal/field/Tests/TranslationTest.php
.../modules/field/lib/Drupal/field/Tests/TranslationTest.php
+6
-47
core/modules/field/lib/Drupal/field/Tests/TranslationWebTest.php
...dules/field/lib/Drupal/field/Tests/TranslationWebTest.php
+109
-0
core/modules/field_sql_storage/lib/Drupal/field_sql_storage/Tests/FieldSqlStorageTest.php
...ib/Drupal/field_sql_storage/Tests/FieldSqlStorageTest.php
+4
-3
core/modules/file/lib/Drupal/file/Tests/FileItemTest.php
core/modules/file/lib/Drupal/file/Tests/FileItemTest.php
+2
-2
core/modules/number/lib/Drupal/number/Tests/NumberItemTest.php
...modules/number/lib/Drupal/number/Tests/NumberItemTest.php
+2
-2
core/modules/options/lib/Drupal/options/Tests/OptionsFieldTest.php
...les/options/lib/Drupal/options/Tests/OptionsFieldTest.php
+3
-5
core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TaxonomyTermReferenceItemTest.php
...b/Drupal/taxonomy/Tests/TaxonomyTermReferenceItemTest.php
+2
-2
core/modules/telephone/lib/Drupal/telephone/Tests/TelephoneItemTest.php
...elephone/lib/Drupal/telephone/Tests/TelephoneItemTest.php
+2
-2
No files found.
core/modules/email/lib/Drupal/email/Tests/EmailItemTest.php
View file @
282f6b90
...
...
@@ -9,12 +9,12 @@
use
Drupal\Core\Entity\Field\FieldInterface
;
use
Drupal\Core\Entity\Field\FieldItemInterface
;
use
Drupal\field\Tests\Field
Item
UnitTestBase
;
use
Drupal\field\Tests\FieldUnitTestBase
;
/**
* Tests the new entity API for the email field type.
*/
class
EmailItemTest
extends
Field
Item
UnitTestBase
{
class
EmailItemTest
extends
FieldUnitTestBase
{
/**
* Modules to enable.
...
...
core/modules/field/lib/Drupal/field/Tests/ActiveTest.php
0 → 100644
View file @
282f6b90
<?php
/**
* @file
* Definition of Drupal\field\Tests\ActiveTest.
*/
namespace
Drupal\field\Tests
;
class
ActiveTest
extends
FieldTestBase
{
/**
* Modules to enable.
*
* @var array
*/
public
static
$modules
=
array
(
'field_test'
);
public
static
function
getInfo
()
{
return
array
(
'name'
=>
'Field active test'
,
'description'
=>
'Test that fields are properly marked active or inactive.'
,
'group'
=>
'Field API'
,
);
}
/**
* Test that fields are properly marked active or inactive.
*/
function
testActive
()
{
$field_definition
=
array
(
'field_name'
=>
'field_1'
,
'type'
=>
'test_field'
,
// For this test, we need a storage backend provided by a different
// module than field_test.module.
'storage'
=>
array
(
'type'
=>
'field_sql_storage'
,
),
);
field_create_field
(
$field_definition
);
// Test disabling and enabling:
// - the field type module,
// - the storage module,
// - both.
$this
->
_testActiveHelper
(
$field_definition
,
array
(
'field_test'
));
$this
->
_testActiveHelper
(
$field_definition
,
array
(
'field_sql_storage'
));
$this
->
_testActiveHelper
(
$field_definition
,
array
(
'field_test'
,
'field_sql_storage'
));
}
/**
* Helper function for testActive().
*
* Test dependency between a field and a set of modules.
*
* @param $field_definition
* A field definition.
* @param $modules
* An aray of module names. The field will be tested to be inactive as long
* as any of those modules is disabled.
*/
function
_testActiveHelper
(
$field_definition
,
$modules
)
{
$field_name
=
$field_definition
[
'field_name'
];
// Read the field.
$field
=
field_read_field
(
$field_name
);
$this
->
assertTrue
(
$field_definition
<=
$field
,
'The field was properly read.'
);
module_disable
(
$modules
,
FALSE
);
$fields
=
field_read_fields
(
array
(
'field_name'
=>
$field_name
),
array
(
'include_inactive'
=>
TRUE
));
$this
->
assertTrue
(
isset
(
$fields
[
$field_name
])
&&
$field_definition
<
$field
,
'The field is properly read when explicitly fetching inactive fields.'
);
// Re-enable modules one by one, and check that the field is still inactive
// while some modules remain disabled.
while
(
$modules
)
{
$field
=
field_read_field
(
$field_name
);
$this
->
assertTrue
(
empty
(
$field
),
format_string
(
'%modules disabled. The field is marked inactive.'
,
array
(
'%modules'
=>
implode
(
', '
,
$modules
))));
$module
=
array_shift
(
$modules
);
module_enable
(
array
(
$module
),
FALSE
);
}
// Check that the field is active again after all modules have been
// enabled.
$field
=
field_read_field
(
$field_name
);
$this
->
assertTrue
(
$field_definition
<=
$field
,
'The field was was marked active.'
);
}
}
core/modules/field/lib/Drupal/field/Tests/BulkDeleteTest.php
View file @
282f6b90
...
...
@@ -10,14 +10,7 @@
/**
* Unit test class for field bulk delete and batch purge functionality.
*/
class
BulkDeleteTest
extends
FieldTestBase
{
/**
* Modules to enable.
*
* @var array
*/
public
static
$modules
=
array
(
'field_test'
);
class
BulkDeleteTest
extends
FieldUnitTestBase
{
protected
$field
;
...
...
core/modules/field/lib/Drupal/field/Tests/CrudTest.php
View file @
282f6b90
...
...
@@ -10,14 +10,14 @@
use
Drupal\field\FieldException
;
use
Exception
;
class
CrudTest
extends
FieldTestBase
{
class
CrudTest
extends
Field
Unit
TestBase
{
/**
* Modules to enable.
*
* @var array
*/
public
static
$modules
=
array
(
'
field_test'
,
'
number'
);
public
static
$modules
=
array
(
'number'
);
public
static
function
getInfo
()
{
return
array
(
...
...
@@ -444,67 +444,4 @@ function testUpdateFieldForbid() {
$this
->
pass
(
t
(
"An unchangeable setting cannot be updated."
));
}
}
/**
* Test that fields are properly marked active or inactive.
*/
function
testActive
()
{
$field_definition
=
array
(
'field_name'
=>
'field_1'
,
'type'
=>
'test_field'
,
// For this test, we need a storage backend provided by a different
// module than field_test.module.
'storage'
=>
array
(
'type'
=>
'field_sql_storage'
,
),
);
field_create_field
(
$field_definition
);
// Test disabling and enabling:
// - the field type module,
// - the storage module,
// - both.
$this
->
_testActiveHelper
(
$field_definition
,
array
(
'field_test'
));
$this
->
_testActiveHelper
(
$field_definition
,
array
(
'field_sql_storage'
));
$this
->
_testActiveHelper
(
$field_definition
,
array
(
'field_test'
,
'field_sql_storage'
));
}
/**
* Helper function for testActive().
*
* Test dependency between a field and a set of modules.
*
* @param $field_definition
* A field definition.
* @param $modules
* An aray of module names. The field will be tested to be inactive as long
* as any of those modules is disabled.
*/
function
_testActiveHelper
(
$field_definition
,
$modules
)
{
$field_name
=
$field_definition
[
'field_name'
];
// Read the field.
$field
=
field_read_field
(
$field_name
);
$this
->
assertTrue
(
$field_definition
<=
$field
,
'The field was properly read.'
);
module_disable
(
$modules
,
FALSE
);
$fields
=
field_read_fields
(
array
(
'field_name'
=>
$field_name
),
array
(
'include_inactive'
=>
TRUE
));
$this
->
assertTrue
(
isset
(
$fields
[
$field_name
])
&&
$field_definition
<
$field
,
'The field is properly read when explicitly fetching inactive fields.'
);
// Re-enable modules one by one, and check that the field is still inactive
// while some modules remain disabled.
while
(
$modules
)
{
$field
=
field_read_field
(
$field_name
);
$this
->
assertTrue
(
empty
(
$field
),
format_string
(
'%modules disabled. The field is marked inactive.'
,
array
(
'%modules'
=>
implode
(
', '
,
$modules
))));
$module
=
array_shift
(
$modules
);
module_enable
(
array
(
$module
),
FALSE
);
}
// Check that the field is active again after all modules have been
// enabled.
$field
=
field_read_field
(
$field_name
);
$this
->
assertTrue
(
$field_definition
<=
$field
,
'The field was was marked active.'
);
}
}
core/modules/field/lib/Drupal/field/Tests/DisplayApiTest.php
View file @
282f6b90
...
...
@@ -7,14 +7,7 @@
namespace
Drupal\field\Tests
;
class
DisplayApiTest
extends
FieldTestBase
{
/**
* Modules to enable.
*
* @var array
*/
public
static
$modules
=
array
(
'field_test'
);
class
DisplayApiTest
extends
FieldUnitTestBase
{
public
static
function
getInfo
()
{
return
array
(
...
...
@@ -84,7 +77,7 @@ function setUp() {
function
testFieldViewField
()
{
// No display settings: check that default display settings are used.
$output
=
field_view_field
(
$this
->
entity
,
$this
->
field_name
);
$this
->
drupalSetContent
(
drupal_render
(
$output
)
);
$this
->
content
=
drupal_render
(
$output
);
$settings
=
field_info_formatter_settings
(
'field_test_default'
);
$setting
=
$settings
[
'test_formatter_setting'
];
$this
->
assertText
(
$this
->
label
,
'Label was displayed.'
);
...
...
@@ -102,7 +95,7 @@ function testFieldViewField() {
),
);
$output
=
field_view_field
(
$this
->
entity
,
$this
->
field_name
,
$display
);
$this
->
drupalSetContent
(
drupal_render
(
$output
)
);
$this
->
content
=
drupal_render
(
$output
);
$setting
=
$display
[
'settings'
][
'test_formatter_setting_multiple'
];
$this
->
assertNoText
(
$this
->
label
,
'Label was not displayed.'
);
$this
->
assertText
(
'field_test_field_attach_view_alter'
,
'Alter fired, display passed.'
);
...
...
@@ -122,7 +115,7 @@ function testFieldViewField() {
);
$output
=
field_view_field
(
$this
->
entity
,
$this
->
field_name
,
$display
);
$view
=
drupal_render
(
$output
);
$this
->
drupalSetContent
(
$view
)
;
$this
->
content
=
$view
;
$setting
=
$display
[
'settings'
][
'test_formatter_setting_additional'
];
$this
->
assertNoText
(
$this
->
label
,
'Label was not displayed.'
);
$this
->
assertNoText
(
'field_test_field_attach_view_alter'
,
'Alter not fired.'
);
...
...
@@ -133,7 +126,7 @@ function testFieldViewField() {
// View mode: check that display settings specified in the display object
// are used.
$output
=
field_view_field
(
$this
->
entity
,
$this
->
field_name
,
'teaser'
);
$this
->
drupalSetContent
(
drupal_render
(
$output
)
);
$this
->
content
=
drupal_render
(
$output
);
$setting
=
$this
->
display_options
[
'teaser'
][
'settings'
][
'test_formatter_setting'
];
$this
->
assertText
(
$this
->
label
,
'Label was displayed.'
);
foreach
(
$this
->
values
as
$delta
=>
$value
)
{
...
...
@@ -143,7 +136,7 @@ function testFieldViewField() {
// Unknown view mode: check that display settings for 'default' view mode
// are used.
$output
=
field_view_field
(
$this
->
entity
,
$this
->
field_name
,
'unknown_view_mode'
);
$this
->
drupalSetContent
(
drupal_render
(
$output
)
);
$this
->
content
=
drupal_render
(
$output
);
$setting
=
$this
->
display_options
[
'default'
][
'settings'
][
'test_formatter_setting'
];
$this
->
assertText
(
$this
->
label
,
'Label was displayed.'
);
foreach
(
$this
->
values
as
$delta
=>
$value
)
{
...
...
@@ -161,7 +154,7 @@ function testFieldViewValue() {
foreach
(
$this
->
values
as
$delta
=>
$value
)
{
$item
=
$this
->
entity
->
{
$this
->
field_name
}[
LANGUAGE_NOT_SPECIFIED
][
$delta
];
$output
=
field_view_value
(
$this
->
entity
,
$this
->
field_name
,
$item
);
$this
->
drupalSetContent
(
drupal_render
(
$output
)
);
$this
->
content
=
drupal_render
(
$output
);
$this
->
assertText
(
$setting
.
'|'
.
$value
[
'value'
],
format_string
(
'Value @delta was displayed with expected setting.'
,
array
(
'@delta'
=>
$delta
)));
}
...
...
@@ -177,7 +170,7 @@ function testFieldViewValue() {
foreach
(
$this
->
values
as
$delta
=>
$value
)
{
$item
=
$this
->
entity
->
{
$this
->
field_name
}[
LANGUAGE_NOT_SPECIFIED
][
$delta
];
$output
=
field_view_value
(
$this
->
entity
,
$this
->
field_name
,
$item
,
$display
);
$this
->
drupalSetContent
(
drupal_render
(
$output
)
);
$this
->
content
=
drupal_render
(
$output
);
$this
->
assertText
(
$setting
.
'|0:'
.
$value
[
'value'
],
format_string
(
'Value @delta was displayed with expected setting.'
,
array
(
'@delta'
=>
$delta
)));
}
...
...
@@ -193,7 +186,7 @@ function testFieldViewValue() {
foreach
(
$this
->
values
as
$delta
=>
$value
)
{
$item
=
$this
->
entity
->
{
$this
->
field_name
}[
LANGUAGE_NOT_SPECIFIED
][
$delta
];
$output
=
field_view_value
(
$this
->
entity
,
$this
->
field_name
,
$item
,
$display
);
$this
->
drupalSetContent
(
drupal_render
(
$output
)
);
$this
->
content
=
drupal_render
(
$output
);
$this
->
assertText
(
$setting
.
'|'
.
$value
[
'value'
]
.
'|'
.
(
$value
[
'value'
]
+
1
),
format_string
(
'Value @delta was displayed with expected setting.'
,
array
(
'@delta'
=>
$delta
)));
}
...
...
@@ -203,7 +196,7 @@ function testFieldViewValue() {
foreach
(
$this
->
values
as
$delta
=>
$value
)
{
$item
=
$this
->
entity
->
{
$this
->
field_name
}[
LANGUAGE_NOT_SPECIFIED
][
$delta
];
$output
=
field_view_value
(
$this
->
entity
,
$this
->
field_name
,
$item
,
'teaser'
);
$this
->
drupalSetContent
(
drupal_render
(
$output
)
);
$this
->
content
=
drupal_render
(
$output
);
$this
->
assertText
(
$setting
.
'|'
.
$value
[
'value'
],
format_string
(
'Value @delta was displayed with expected setting.'
,
array
(
'@delta'
=>
$delta
)));
}
...
...
@@ -213,7 +206,7 @@ function testFieldViewValue() {
foreach
(
$this
->
values
as
$delta
=>
$value
)
{
$item
=
$this
->
entity
->
{
$this
->
field_name
}[
LANGUAGE_NOT_SPECIFIED
][
$delta
];
$output
=
field_view_value
(
$this
->
entity
,
$this
->
field_name
,
$item
,
'unknown_view_mode'
);
$this
->
drupalSetContent
(
drupal_render
(
$output
)
);
$this
->
content
=
drupal_render
(
$output
);
$this
->
assertText
(
$setting
.
'|'
.
$value
[
'value'
],
format_string
(
'Value @delta was displayed with expected setting.'
,
array
(
'@delta'
=>
$delta
)));
}
}
...
...
core/modules/field/lib/Drupal/field/Tests/FieldAttachOtherTest.php
View file @
282f6b90
...
...
@@ -12,7 +12,7 @@
/**
* Unit test class for non-storage related field_attach_* functions.
*/
class
FieldAttachOtherTest
extends
Field
Attach
TestBase
{
class
FieldAttachOtherTest
extends
Field
Unit
TestBase
{
public
static
function
getInfo
()
{
return
array
(
'name'
=>
'Field attach tests (other)'
,
...
...
@@ -21,6 +21,11 @@ public static function getInfo() {
);
}
public
function
setUp
()
{
parent
::
setUp
();
$this
->
createFieldWithInstance
();
}
/**
* Test field_attach_view() and field_attach_prepare_view().
*/
...
...
core/modules/field/lib/Drupal/field/Tests/FieldAttachStorageTest.php
View file @
282f6b90
...
...
@@ -13,7 +13,7 @@
* All field_attach_* test work with all field_storage plugins and
* all hook_field_attach_pre_{load,insert,update}() hooks.
*/
class
FieldAttachStorageTest
extends
Field
Attach
TestBase
{
class
FieldAttachStorageTest
extends
Field
Unit
TestBase
{
public
static
function
getInfo
()
{
return
array
(
'name'
=>
'Field attach tests (storage-related)'
,
...
...
@@ -22,6 +22,11 @@ public static function getInfo() {
);
}
public
function
setUp
()
{
parent
::
setUp
();
$this
->
createFieldWithInstance
();
}
/**
* Check field values insert, update and load.
*
...
...
core/modules/field/lib/Drupal/field/Tests/FieldAttachTestBase.php
deleted
100644 → 0
View file @
80fd0f97
<?php
/**
* @file
* Definition of Drupal\field\Tests\FieldAttachTestBase.
*/
namespace
Drupal\field\Tests
;
abstract
class
FieldAttachTestBase
extends
FieldTestBase
{
/**
* Modules to enable.
*
* @var array
*/
public
static
$modules
=
array
(
'field_test'
);
function
setUp
()
{
parent
::
setUp
();
$this
->
createFieldWithInstance
();
}
/**
* Create a field and an instance of it.
*
* @param string $suffix
* (optional) A string that should only contain characters that are valid in
* PHP variable names as well.
*/
function
createFieldWithInstance
(
$suffix
=
''
)
{
$field_name
=
'field_name'
.
$suffix
;
$field
=
'field'
.
$suffix
;
$field_id
=
'field_id'
.
$suffix
;
$instance
=
'instance'
.
$suffix
;
$this
->
$field_name
=
drupal_strtolower
(
$this
->
randomName
()
.
'_field_name'
.
$suffix
);
$this
->
$field
=
array
(
'field_name'
=>
$this
->
$field_name
,
'type'
=>
'test_field'
,
'cardinality'
=>
4
);
$this
->
$field
=
field_create_field
(
$this
->
$field
);
$this
->
$field_id
=
$this
->
{
$field
}[
'id'
];
$this
->
$instance
=
array
(
'field_name'
=>
$this
->
$field_name
,
'entity_type'
=>
'test_entity'
,
'bundle'
=>
'test_bundle'
,
'label'
=>
$this
->
randomName
()
.
'_label'
,
'description'
=>
$this
->
randomName
()
.
'_description'
,
'weight'
=>
mt_rand
(
0
,
127
),
'settings'
=>
array
(
'test_instance_setting'
=>
$this
->
randomName
(),
),
'widget'
=>
array
(
'type'
=>
'test_field_widget'
,
'label'
=>
'Test Field'
,
'settings'
=>
array
(
'test_widget_setting'
=>
$this
->
randomName
(),
)
)
);
field_create_instance
(
$this
->
$instance
);
}
}
core/modules/field/lib/Drupal/field/Tests/FieldInfoTest.php
View file @
282f6b90
...
...
@@ -7,14 +7,7 @@
namespace
Drupal\field\Tests
;
class
FieldInfoTest
extends
FieldTestBase
{
/**
* Modules to enable.
*
* @var array
*/
public
static
$modules
=
array
(
'field_test'
);
class
FieldInfoTest
extends
FieldUnitTestBase
{
public
static
function
getInfo
()
{
return
array
(
...
...
core/modules/field/lib/Drupal/field/Tests/FieldInstanceCrudTest.php
View file @
282f6b90
...
...
@@ -9,14 +9,7 @@
use
Drupal\field\FieldException
;
class
FieldInstanceCrudTest
extends
FieldTestBase
{
/**
* Modules to enable.
*
* @var array
*/
public
static
$modules
=
array
(
'field_test'
);
class
FieldInstanceCrudTest
extends
FieldUnitTestBase
{
protected
$field
;
...
...
core/modules/field/lib/Drupal/field/Tests/FieldItemUnitTestBase.php
deleted
100644 → 0
View file @
80fd0f97
<?php
/**
* @file
* Contains \Drupal\field\Tests\FieldItemUnitTestBase.
*/
namespace
Drupal\field\Tests
;
use
Drupal\simpletest\DrupalUnitTestBase
;
/**
* Base test class for field type item tests.
*/
class
FieldItemUnitTestBase
extends
DrupalUnitTestBase
{
/**
* Modules to enable.
*
* @var array
*/
public
static
$modules
=
array
(
'user'
,
'system'
,
'field'
,
'text'
,
'field_sql_storage'
,
'field_test'
,
'entity_test'
);
public
function
setUp
()
{
parent
::
setUp
();
$this
->
installSchema
(
'system'
,
'sequences'
);
$this
->
installSchema
(
'field'
,
'field_config'
);
$this
->
installSchema
(
'field'
,
'field_config_instance'
);
$this
->
installSchema
(
'entity_test'
,
'entity_test'
);
}
}
core/modules/field/lib/Drupal/field/Tests/FieldUnitTestBase.php
0 → 100644
View file @
282f6b90
<?php
/**
* @file
* Contains \Drupal\field\Tests\FieldUnitTestBase.
*/
namespace
Drupal\field\Tests
;
use
Drupal\Core\Entity\EntityInterface
;
use
Drupal\simpletest\DrupalUnitTestBase
;
/**
* Parent class for Field API unit tests.
*/
abstract
class
FieldUnitTestBase
extends
DrupalUnitTestBase
{
var
$default_storage
=
'field_sql_storage'
;
/**
* Modules to enable.
*
* @var array
*/
public
static
$modules
=
array
(
'user'
,
'entity'
,
'system'
,
'field'
,
'text'
,
'field_sql_storage'
,
'entity_test'
,
'field_test'
);
/**
* A string for assert raw and text helper methods.
*
* @var string
*/
protected
$content
;
/**
* Set the default field storage backend for fields created during tests.
*/
function
setUp
()
{
parent
::
setUp
();
$this
->
installSchema
(
'system'
,
array
(
'sequences'
,
'variable'
));
$this
->
installSchema
(
'field'
,
array
(
'field_config'
,
'field_config_instance'
));
$this
->
installSchema
(
'entity_test'
,
'entity_test'
);
$this
->
installSchema
(
'field_test'
,
array
(
'test_entity'
,
'test_entity_revision'
,
'test_entity_bundle'
));
// Set default storage backend.
variable_set
(
'field_storage_default'
,
$this
->
default_storage
);
}
/**
* Create a field and an instance of it.
*
* @param string $suffix
* (optional) A string that should only contain characters that are valid in
* PHP variable names as well.
*/
function
createFieldWithInstance
(
$suffix
=
''
)
{
$field_name
=
'field_name'
.
$suffix
;
$field
=
'field'
.
$suffix
;
$field_id
=
'field_id'
.
$suffix
;
$instance
=
'instance'
.
$suffix
;
$this
->
$field_name
=
drupal_strtolower
(
$this
->
randomName
()
.
'_field_name'
.
$suffix
);
$this
->
$field
=
array
(
'field_name'
=>
$this
->
$field_name
,
'type'
=>
'test_field'
,
'cardinality'
=>
4
);
$this
->
$field
=
field_create_field
(
$this
->
$field
);
$this
->
$field_id
=
$this
->
{
$field
}[
'id'
];
$this
->
$instance
=
array
(
'field_name'
=>
$this
->
$field_name
,
'entity_type'
=>
'test_entity'
,
'bundle'
=>
'test_bundle'
,
'label'
=>
$this
->
randomName
()
.
'_label'
,
'description'
=>
$this
->
randomName
()
.
'_description'
,
'weight'
=>
mt_rand
(
0
,
127
),
'settings'
=>
array
(
'test_instance_setting'
=>
$this
->
randomName
(),
),
'widget'
=>
array
(
'type'
=>
'test_field_widget'
,
'label'
=>
'Test Field'
,
'settings'
=>
array
(
'test_widget_setting'
=>
$this
->
randomName
(),
)
)
);
field_create_instance
(
$this
->
$instance
);
}
/**
* Generate random values for a field_test field.
*
* @param $cardinality
* Number of values to generate.
* @return
* An array of random values, in the format expected for field values.
*/
function
_generateTestFieldValues
(
$cardinality
)
{
$values
=
array
();
for
(
$i
=
0
;
$i
<
$cardinality
;
$i
++
)
{
// field_test fields treat 0 as 'empty value'.
$values
[
$i
][
'value'
]
=
mt_rand
(
1
,
127
);
}
return
$values
;
}
/**
* Assert that a field has the expected values in an entity.
*
* This function only checks a single column in the field values.
*
* @param EntityInterface $entity