Révision b0dc3a2e
Ajouté par Julien Enselme il y a plus de 7 ans
drupal7/modules/file/tests/file.test | ||
---|---|---|
22 | 22 |
$modules[] = 'file'; |
23 | 23 |
$modules[] = 'file_module_test'; |
24 | 24 |
parent::setUp($modules); |
25 |
$this->admin_user = $this->drupalCreateUser(array('access content', 'access administration pages', 'administer site configuration', 'administer users', 'administer permissions', 'administer content types', 'administer nodes', 'bypass node access')); |
|
25 |
$this->admin_user = $this->drupalCreateUser(array('access content', 'access administration pages', 'administer site configuration', 'administer users', 'administer permissions', 'administer content types', 'administer nodes', 'bypass node access', 'administer fields'));
|
|
26 | 26 |
$this->drupalLogin($this->admin_user); |
27 | 27 |
} |
28 | 28 |
|
... | ... | |
1503 | 1503 |
$this->assertResponse(403, 'Confirmed that access is denied for the file without view field access permission after attempting to attach it to a new node.'); |
1504 | 1504 |
} |
1505 | 1505 |
} |
1506 |
|
|
1507 |
/** |
|
1508 |
* Confirm that file field submissions work correctly for anonymous visitors. |
|
1509 |
*/ |
|
1510 |
class FileFieldAnonymousSubmission extends FileFieldTestCase { |
|
1511 |
|
|
1512 |
public static function getInfo() { |
|
1513 |
return array( |
|
1514 |
'name' => 'File form anonymous submission', |
|
1515 |
'description' => 'Test anonymous form submission.', |
|
1516 |
'group' => 'File', |
|
1517 |
); |
|
1518 |
} |
|
1519 |
|
|
1520 |
function setUp() { |
|
1521 |
parent::setUp(); |
|
1522 |
|
|
1523 |
// Allow node submissions by anonymous users. |
|
1524 |
user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, array( |
|
1525 |
'create article content', |
|
1526 |
'access content', |
|
1527 |
)); |
|
1528 |
} |
|
1529 |
|
|
1530 |
/** |
|
1531 |
* Tests the basic node submission for an anonymous visitor. |
|
1532 |
*/ |
|
1533 |
function testAnonymousNode() { |
|
1534 |
$bundle_label = 'Article'; |
|
1535 |
$node_title = 'Test page'; |
|
1536 |
|
|
1537 |
// Load the node form. |
|
1538 |
$this->drupalGet('node/add/article'); |
|
1539 |
$this->assertResponse(200, 'Loaded the article node form.'); |
|
1540 |
$this->assertText(strip_tags(t('Create @name', array('@name' => $bundle_label)))); |
|
1541 |
|
|
1542 |
$edit = array( |
|
1543 |
'title' => $node_title, |
|
1544 |
'body[und][0][value]' => 'Test article', |
|
1545 |
'body[und][0][format]' => 'filtered_html', |
|
1546 |
); |
|
1547 |
$this->drupalPost(NULL, $edit, t('Save')); |
|
1548 |
$this->assertResponse(200); |
|
1549 |
$t_args = array('@type' => $bundle_label, '%title' => $node_title); |
|
1550 |
$this->assertText(strip_tags(t('@type %title has been created.', $t_args)), 'The node was created.'); |
|
1551 |
$matches = array(); |
|
1552 |
if (preg_match('@node/(\d+)$@', $this->getUrl(), $matches)) { |
|
1553 |
$nid = end($matches); |
|
1554 |
$this->assertNotEqual($nid, 0, 'The node ID was extracted from the URL.'); |
|
1555 |
$node = node_load($nid); |
|
1556 |
$this->assertNotEqual($node, NULL, 'The node was loaded successfully.'); |
|
1557 |
} |
|
1558 |
} |
|
1559 |
|
|
1560 |
/** |
|
1561 |
* Tests file submission for an anonymous visitor. |
|
1562 |
*/ |
|
1563 |
function testAnonymousNodeWithFile() { |
|
1564 |
$bundle_label = 'Article'; |
|
1565 |
$node_title = 'Test page'; |
|
1566 |
|
|
1567 |
// Load the node form. |
|
1568 |
$this->drupalGet('node/add/article'); |
|
1569 |
$this->assertResponse(200, 'Loaded the article node form.'); |
|
1570 |
$this->assertText(strip_tags(t('Create @name', array('@name' => $bundle_label)))); |
|
1571 |
|
|
1572 |
// Generate an image file. |
|
1573 |
$image = $this->getTestImage(); |
|
1574 |
|
|
1575 |
// Submit the form. |
|
1576 |
$edit = array( |
|
1577 |
'title' => $node_title, |
|
1578 |
'body[und][0][value]' => 'Test article', |
|
1579 |
'body[und][0][format]' => 'filtered_html', |
|
1580 |
'files[field_image_und_0]' => drupal_realpath($image->uri), |
|
1581 |
); |
|
1582 |
$this->drupalPost(NULL, $edit, t('Save')); |
|
1583 |
$this->assertResponse(200); |
|
1584 |
$t_args = array('@type' => $bundle_label, '%title' => $node_title); |
|
1585 |
$this->assertText(strip_tags(t('@type %title has been created.', $t_args)), 'The node was created.'); |
|
1586 |
$matches = array(); |
|
1587 |
if (preg_match('@node/(\d+)$@', $this->getUrl(), $matches)) { |
|
1588 |
$nid = end($matches); |
|
1589 |
$this->assertNotEqual($nid, 0, 'The node ID was extracted from the URL.'); |
|
1590 |
$node = node_load($nid); |
|
1591 |
$this->assertNotEqual($node, NULL, 'The node was loaded successfully.'); |
|
1592 |
$this->assertEqual($node->field_image[LANGUAGE_NONE][0]['filename'], $image->filename, 'The image was uploaded successfully.'); |
|
1593 |
} |
|
1594 |
} |
|
1595 |
|
|
1596 |
/** |
|
1597 |
* Tests file submission for an anonymous visitor with a missing node title. |
|
1598 |
*/ |
|
1599 |
function testAnonymousNodeWithFileWithoutTitle() { |
|
1600 |
$this->drupalLogout(); |
|
1601 |
$this->_testNodeWithFileWithoutTitle(); |
|
1602 |
} |
|
1603 |
|
|
1604 |
/** |
|
1605 |
* Tests file submission for an authenticated user with a missing node title. |
|
1606 |
*/ |
|
1607 |
function testAuthenticatedNodeWithFileWithoutTitle() { |
|
1608 |
$admin_user = $this->drupalCreateUser(array( |
|
1609 |
'bypass node access', |
|
1610 |
'access content overview', |
|
1611 |
'administer nodes', |
|
1612 |
)); |
|
1613 |
$this->drupalLogin($admin_user); |
|
1614 |
$this->_testNodeWithFileWithoutTitle(); |
|
1615 |
} |
|
1616 |
|
|
1617 |
/** |
|
1618 |
* Helper method to test file submissions with missing node titles. |
|
1619 |
*/ |
|
1620 |
protected function _testNodeWithFileWithoutTitle() { |
|
1621 |
$bundle_label = 'Article'; |
|
1622 |
$node_title = 'Test page'; |
|
1623 |
|
|
1624 |
// Load the node form. |
|
1625 |
$this->drupalGet('node/add/article'); |
|
1626 |
$this->assertResponse(200, 'Loaded the article node form.'); |
|
1627 |
$this->assertText(strip_tags(t('Create @name', array('@name' => $bundle_label)))); |
|
1628 |
|
|
1629 |
// Generate an image file. |
|
1630 |
$image = $this->getTestImage(); |
|
1631 |
|
|
1632 |
// Submit the form but exclude the title field. |
|
1633 |
$edit = array( |
|
1634 |
'body[und][0][value]' => 'Test article', |
|
1635 |
'body[und][0][format]' => 'filtered_html', |
|
1636 |
'files[field_image_und_0]' => drupal_realpath($image->uri), |
|
1637 |
); |
|
1638 |
$this->drupalPost(NULL, $edit, t('Save')); |
|
1639 |
$this->assertResponse(200); |
|
1640 |
$t_args = array('@type' => $bundle_label, '%title' => $node_title); |
|
1641 |
$this->assertNoText(strip_tags(t('@type %title has been created.', $t_args)), 'The node was created.'); |
|
1642 |
$this->assertText(t('!name field is required.', array('!name' => t('Title')))); |
|
1643 |
|
|
1644 |
// Submit the form again but this time with the missing title field. This |
|
1645 |
// should still work. |
|
1646 |
$edit = array( |
|
1647 |
'title' => $node_title, |
|
1648 |
); |
|
1649 |
$this->drupalPost(NULL, $edit, t('Save')); |
|
1650 |
|
|
1651 |
// Confirm the final submission actually worked. |
|
1652 |
$t_args = array('@type' => $bundle_label, '%title' => $node_title); |
|
1653 |
$this->assertText(strip_tags(t('@type %title has been created.', $t_args)), 'The node was created.'); |
|
1654 |
$matches = array(); |
|
1655 |
if (preg_match('@node/(\d+)$@', $this->getUrl(), $matches)) { |
|
1656 |
$nid = end($matches); |
|
1657 |
$this->assertNotEqual($nid, 0, 'The node ID was extracted from the URL.'); |
|
1658 |
$node = node_load($nid); |
|
1659 |
$this->assertNotEqual($node, NULL, 'The node was loaded successfully.'); |
|
1660 |
$this->assertEqual($node->field_image[LANGUAGE_NONE][0]['filename'], $image->filename, 'The image was uploaded successfully.'); |
|
1661 |
} |
|
1662 |
} |
|
1663 |
|
|
1664 |
/** |
|
1665 |
* Generates a test image. |
|
1666 |
* |
|
1667 |
* @return stdClass |
|
1668 |
* A file object. |
|
1669 |
*/ |
|
1670 |
function getTestImage() { |
|
1671 |
// Get a file to upload. |
|
1672 |
$file = current($this->drupalGetTestFiles('image')); |
|
1673 |
|
|
1674 |
// Add a filesize property to files as would be read by file_load(). |
|
1675 |
$file->filesize = filesize($file->uri); |
|
1676 |
|
|
1677 |
return $file; |
|
1678 |
} |
|
1679 |
|
|
1680 |
} |
Formats disponibles : Unified diff
Update to Drupal 7.52