FINAL suepr merge step : added all modules to this super repos

This commit is contained in:
Bachir Soussi Chiadmi
2015-04-19 16:46:59 +02:00
7585 changed files with 1723356 additions and 18 deletions

View File

@@ -0,0 +1,93 @@
/**
* Revisions log for unique_field
*/
version 7.x-1.0-rc1 (2011/07/25)
-- #1163924 by derhasi: fixing support for multilingual fields
-- #1185904: reworking query code to use EntityFieldQuery
-- #1185902: adding functional tests for core functionality
version 2011/06/11 (7.x-1.0-beta2)
-- #1037904 by Boobaa: fixing compatibility with localized content
version 2010/11/20 (7.x-1.0-beta1)
-- #945482 by Scott Falconer: fixing PDOException on empty values in
multi-value fields
-- #970548: fixing undefined index warning
-- fixing JavaScript bypass for compatibility with Drupal 7 updates
-- other code improvements
version 2010/08/04
-- fixed restriction bypass JavaScript for Drupal 7 compatibility
version 2010/01/20
-- validation is skipped when a node is deleted (otherwise nodes
with duplicate values are prevented from being deleted)
-- fixed translatability of all strings
-- added documentation through help hook
-- fixed compatibility with Drupal 7.0 alpha 1
version 2009/11/17
-- initial release of Drupal 7.x compatible version
version 2009/11/12
-- added Italian translation [thanks to eliosh]
-- improved user message that reports duplicate values when checking
fields in combination
-- fixed support for datetime and other date-related database field
datatypes [thanks to Parkes Design]
-- added permission that allows users to bypass the unique field
requirements after getting the warning message [thanks to benjah]
version 2009/03/06
-- fixed query generation for grouping OR clauses
-- altered appearance of unique fields settings on the content type form:
moved unique field section to the bottom, added descriptions
-- fixed compatibility with date module version 6.x-2.0
-- fixed compatibility with content_permissions module [thanks to skassam]
-- Hungarian translation added [thanks to Zoltan Balogh]
version 2009/02/09
-- improved handling of CCK values in single node scope
-- fixed formatting bug in duplicate error message
version 2009/02/07
-- fixed handling of multiple values per CCK field
-- added option for showing the nodes that match when a duplicate
value is found [thanks to crea]
-- added node language as a query field and scope
-- fixed handling of node revisions
-- rewrote query generation routine
-- added node author as a query field
-- added hook_uninstall support for removing the module's variables
[thanks to markus_petrux]
-- improved compliance with coding standards [thanks to deekayen]
version 2008/10/24
-- updated to compatibility with Drupal 6.x [thanks to Yur]
-- added check for content.module for CCK-dependent code [thanks to sigsby]
-- removed all MySQL database-specific code [thanks to rssaddict]
version 2008/03/15
-- fixed to use node title label from node type data [thanks to tj2653]
version 2008/03/14
-- changed behavior so that if a field value is null or empty it matches
no other node (including other nodes with null or empty values)
[thanks to scedwar]
version 2008/03/13
-- fixed support for node and user reference fields in single node scope,
verified support for both in other scopes
version 2008/03/12
-- added single node scope to require that specified fields on a node
have unique values
-- improved error messages to specify which fields must be unique
and which include duplicates
version 2008/02/22
-- fixed queries to support table prefixes
version 2008/02/18
-- initial release

View File

@@ -0,0 +1,274 @@
GNU GENERAL PUBLIC LICENSE
Version 2, June 1991
Copyright (C) 1989, 1991 Free Software Foundation, Inc. 675 Mass Ave,
Cambridge, MA 02139, USA. Everyone is permitted to copy and distribute
verbatim copies of this license document, but changing it is not allowed.
Preamble
The licenses for most software are designed to take away your freedom to
share and change it. By contrast, the GNU General Public License is
intended to guarantee your freedom to share and change free software--to
make sure the software is free for all its users. This General Public License
applies to most of the Free Software Foundation's software and to any other
program whose authors commit to using it. (Some other Free Software
Foundation software is covered by the GNU Library General Public License
instead.) You can apply it to your programs, too.
When we speak of free software, we are referring to freedom, not price. Our
General Public Licenses are designed to make sure that you have the
freedom to distribute copies of free software (and charge for this service if
you wish), that you receive source code or can get it if you want it, that you
can change the software or use pieces of it in new free programs; and that
you know you can do these things.
To protect your rights, we need to make restrictions that forbid anyone to
deny you these rights or to ask you to surrender the rights. These restrictions
translate to certain responsibilities for you if you distribute copies of the
software, or if you modify it.
For example, if you distribute copies of such a program, whether gratis or for
a fee, you must give the recipients all the rights that you have. You must make
sure that they, too, receive or can get the source code. And you must show
them these terms so they know their rights.
We protect your rights with two steps: (1) copyright the software, and (2)
offer you this license which gives you legal permission to copy, distribute
and/or modify the software.
Also, for each author's protection and ours, we want to make certain that
everyone understands that there is no warranty for this free software. If the
software is modified by someone else and passed on, we want its recipients
to know that what they have is not the original, so that any problems
introduced by others will not reflect on the original authors' reputations.
Finally, any free program is threatened constantly by software patents. We
wish to avoid the danger that redistributors of a free program will individually
obtain patent licenses, in effect making the program proprietary. To prevent
this, we have made it clear that any patent must be licensed for everyone's
free use or not licensed at all.
The precise terms and conditions for copying, distribution and modification
follow.
GNU GENERAL PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND
MODIFICATION
0. This License applies to any program or other work which contains a notice
placed by the copyright holder saying it may be distributed under the terms
of this General Public License. The "Program", below, refers to any such
program or work, and a "work based on the Program" means either the
Program or any derivative work under copyright law: that is to say, a work
containing the Program or a portion of it, either verbatim or with
modifications and/or translated into another language. (Hereinafter, translation
is included without limitation in the term "modification".) Each licensee is
addressed as "you".
Activities other than copying, distribution and modification are not covered
by this License; they are outside its scope. The act of running the Program is
not restricted, and the output from the Program is covered only if its contents
constitute a work based on the Program (independent of having been made
by running the Program). Whether that is true depends on what the Program
does.
1. You may copy and distribute verbatim copies of the Program's source
code as you receive it, in any medium, provided that you conspicuously and
appropriately publish on each copy an appropriate copyright notice and
disclaimer of warranty; keep intact all the notices that refer to this License
and to the absence of any warranty; and give any other recipients of the
Program a copy of this License along with the Program.
You may charge a fee for the physical act of transferring a copy, and you
may at your option offer warranty protection in exchange for a fee.
2. You may modify your copy or copies of the Program or any portion of it,
thus forming a work based on the Program, and copy and distribute such
modifications or work under the terms of Section 1 above, provided that you
also meet all of these conditions:
a) You must cause the modified files to carry prominent notices stating that
you changed the files and the date of any change.
b) You must cause any work that you distribute or publish, that in whole or in
part contains or is derived from the Program or any part thereof, to be
licensed as a whole at no charge to all third parties under the terms of this
License.
c) If the modified program normally reads commands interactively when run,
you must cause it, when started running for such interactive use in the most
ordinary way, to print or display an announcement including an appropriate
copyright notice and a notice that there is no warranty (or else, saying that
you provide a warranty) and that users may redistribute the program under
these conditions, and telling the user how to view a copy of this License.
(Exception: if the Program itself is interactive but does not normally print such
an announcement, your work based on the Program is not required to print
an announcement.)
These requirements apply to the modified work as a whole. If identifiable
sections of that work are not derived from the Program, and can be
reasonably considered independent and separate works in themselves, then
this License, and its terms, do not apply to those sections when you distribute
them as separate works. But when you distribute the same sections as part
of a whole which is a work based on the Program, the distribution of the
whole must be on the terms of this License, whose permissions for other
licensees extend to the entire whole, and thus to each and every part
regardless of who wrote it.
Thus, it is not the intent of this section to claim rights or contest your rights to
work written entirely by you; rather, the intent is to exercise the right to
control the distribution of derivative or collective works based on the
Program.
In addition, mere aggregation of another work not based on the Program
with the Program (or with a work based on the Program) on a volume of a
storage or distribution medium does not bring the other work under the scope
of this License.
3. You may copy and distribute the Program (or a work based on it, under
Section 2) in object code or executable form under the terms of Sections 1
and 2 above provided that you also do one of the following:
a) Accompany it with the complete corresponding machine-readable source
code, which must be distributed under the terms of Sections 1 and 2 above
on a medium customarily used for software interchange; or,
b) Accompany it with a written offer, valid for at least three years, to give
any third party, for a charge no more than your cost of physically performing
source distribution, a complete machine-readable copy of the corresponding
source code, to be distributed under the terms of Sections 1 and 2 above on
a medium customarily used for software interchange; or,
c) Accompany it with the information you received as to the offer to distribute
corresponding source code. (This alternative is allowed only for
noncommercial distribution and only if you received the program in object
code or executable form with such an offer, in accord with Subsection b
above.)
The source code for a work means the preferred form of the work for
making modifications to it. For an executable work, complete source code
means all the source code for all modules it contains, plus any associated
interface definition files, plus the scripts used to control compilation and
installation of the executable. However, as a special exception, the source
code distributed need not include anything that is normally distributed (in
either source or binary form) with the major components (compiler, kernel,
and so on) of the operating system on which the executable runs, unless that
component itself accompanies the executable.
If distribution of executable or object code is made by offering access to
copy from a designated place, then offering equivalent access to copy the
source code from the same place counts as distribution of the source code,
even though third parties are not compelled to copy the source along with the
object code.
4. You may not copy, modify, sublicense, or distribute the Program except as
expressly provided under this License. Any attempt otherwise to copy,
modify, sublicense or distribute the Program is void, and will automatically
terminate your rights under this License. However, parties who have received
copies, or rights, from you under this License will not have their licenses
terminated so long as such parties remain in full compliance.
5. You are not required to accept this License, since you have not signed it.
However, nothing else grants you permission to modify or distribute the
Program or its derivative works. These actions are prohibited by law if you
do not accept this License. Therefore, by modifying or distributing the
Program (or any work based on the Program), you indicate your acceptance
of this License to do so, and all its terms and conditions for copying,
distributing or modifying the Program or works based on it.
6. Each time you redistribute the Program (or any work based on the
Program), the recipient automatically receives a license from the original
licensor to copy, distribute or modify the Program subject to these terms and
conditions. You may not impose any further restrictions on the recipients'
exercise of the rights granted herein. You are not responsible for enforcing
compliance by third parties to this License.
7. If, as a consequence of a court judgment or allegation of patent
infringement or for any other reason (not limited to patent issues), conditions
are imposed on you (whether by court order, agreement or otherwise) that
contradict the conditions of this License, they do not excuse you from the
conditions of this License. If you cannot distribute so as to satisfy
simultaneously your obligations under this License and any other pertinent
obligations, then as a consequence you may not distribute the Program at all.
For example, if a patent license would not permit royalty-free redistribution
of the Program by all those who receive copies directly or indirectly through
you, then the only way you could satisfy both it and this License would be to
refrain entirely from distribution of the Program.
If any portion of this section is held invalid or unenforceable under any
particular circumstance, the balance of the section is intended to apply and
the section as a whole is intended to apply in other circumstances.
It is not the purpose of this section to induce you to infringe any patents or
other property right claims or to contest validity of any such claims; this
section has the sole purpose of protecting the integrity of the free software
distribution system, which is implemented by public license practices. Many
people have made generous contributions to the wide range of software
distributed through that system in reliance on consistent application of that
system; it is up to the author/donor to decide if he or she is willing to
distribute software through any other system and a licensee cannot impose
that choice.
This section is intended to make thoroughly clear what is believed to be a
consequence of the rest of this License.
8. If the distribution and/or use of the Program is restricted in certain
countries either by patents or by copyrighted interfaces, the original copyright
holder who places the Program under this License may add an explicit
geographical distribution limitation excluding those countries, so that
distribution is permitted only in or among countries not thus excluded. In such
case, this License incorporates the limitation as if written in the body of this
License.
9. The Free Software Foundation may publish revised and/or new versions
of the General Public License from time to time. Such new versions will be
similar in spirit to the present version, but may differ in detail to address new
problems or concerns.
Each version is given a distinguishing version number. If the Program specifies
a version number of this License which applies to it and "any later version",
you have the option of following the terms and conditions either of that
version or of any later version published by the Free Software Foundation. If
the Program does not specify a version number of this License, you may
choose any version ever published by the Free Software Foundation.
10. If you wish to incorporate parts of the Program into other free programs
whose distribution conditions are different, write to the author to ask for
permission. For software which is copyrighted by the Free Software
Foundation, write to the Free Software Foundation; we sometimes make
exceptions for this. Our decision will be guided by the two goals of
preserving the free status of all derivatives of our free software and of
promoting the sharing and reuse of software generally.
NO WARRANTY
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE,
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT
PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE
STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR
OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT
WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND
PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
NECESSARY SERVICING, REPAIR OR CORRECTION.
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR
AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR
ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE
LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL,
SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES
ARISING OUT OF THE USE OR INABILITY TO USE THE
PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA
OR DATA BEING RENDERED INACCURATE OR LOSSES
SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE
PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN
IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF
THE POSSIBILITY OF SUCH DAMAGES.
END OF TERMS AND CONDITIONS

View File

@@ -0,0 +1,33 @@
/**
* Unique Field module for Drupal (unique_field)
* Compatible with Drupal 7.x
*
* By Joe Turgeon [http://arithmetric.com]
*/
The Unique Field module provides a way to require that specified fields
or characteristics of a node are unique. This includes the node's title,
author, language, taxonomy terms, and other fields.
Without this module, Drupal and CCK do not prevent multiple nodes from
having the same title or the same value in a given field.
For example, if you have a content type with a Date field and there
should only be one node per date, you could use this module to prevent a
node from being saved with a date already used in another node.
This module adds additional options to the administration page for each
content type (i.e. admin/structure/types/manage/<content type>) for
specifying which fields must be unique. The administrator may specify
whether each field must be unique or whether the fields in combination must
be unique. Also, the administrator can choose whether the fields must be
unique among all other nodes or only among nodes from the given node's
content type.
Alternatively, you can select the 'single node' scope, which allows you
to require that the specified fields are each unique on that node. For
example, if a node has multiple, separate user reference fields, this
setting will require that no user is selected more than once on one node.
For more information, see this module's page at:
http://drupal.org/project/unique_field

View File

@@ -0,0 +1,506 @@
<?php
/**
* @file
* Functional tests for the Unique Field module with Drupal core field types.
*
* The basic model for the tests:
*
* - Test each supported field type
* - Test each scope (by content type, language, all, or single node)
* - Test multiple fields individually and in combination
*/
class UniqueFieldCoreTestCase extends DrupalWebTestCase {
protected $privileged_user;
public static function getInfo() {
return array(
'name' => 'Unique Field: Core tests',
'description' => 'Ensure that the Unique Field module functions properly with Drupal core field types.',
'group' => 'Unique Field',
);
}
public function setUp() {
parent::setUp('field', 'field_ui', 'number', 'text', 'unique_field');
// TODO: add tests for other types of fields: date, file, link, number, node
// reference, user reference, etc.
// Create and log in our privileged user.
$this->privileged_user = $this->drupalCreateUser(array(
'administer content types', 'administer nodes', 'bypass node access', 'unique_field_perm_admin', 'unique_field_perm_bypass',
));
$this->drupalLogin($this->privileged_user);
}
/**
* Test the unique requirement on the title field in the content type scope.
*/
public function testUniqueCtypeTitle() {
// Create a content type with the title set to be unique
$edit = array();
$edit['name'] = 'Unique Title';
$edit['type'] = 'uf_title';
$edit['unique_field_fields[title]'] = 'title';
$this->drupalPost('admin/structure/types/add', $edit, t('Save content type'));
$this->assertText('The content type Unique Title has been added.', 'Content type added.');
// Attempt to create 2 nodes with the same title
$title = $this->randomName(24);
$edit = array();
$edit['title'] = $title;
$edit['body[und][0][value]'] = $this->randomName(48);
$this->drupalPost('node/add/uf-title', $edit, t('Save'));
$this->assertText($edit['body[und][0][value]'], 'Unique Title (uf_title) node has been created');
$edit['body[und][0][value]'] = $this->randomName(48);
$this->drupalPost('node/add/uf-title', $edit, t('Save'));
$this->assertText('The Title field requires a unique value, and the specified value is already used', 'Unique Title (uf_title) node with duplicate content could not be created');
// Check for false negative: Attempt to create a node with a unique title
$edit = array();
$edit['title'] = $this->randomName(24);
$edit['body[und][0][value]'] = $this->randomName(48);
$this->drupalPost('node/add/uf-title', $edit, t('Save'));
$this->assertText($edit['body[und][0][value]'], 'Unique Title (uf_title) node has been created');
}
/**
* Test the unique requirement on the title field in the all scope.
*/
public function testUniqueAllTitle() {
// Create a content type with the title set to be unique
$edit = array();
$edit['name'] = 'Unique Title';
$edit['type'] = 'uf_title';
$edit['unique_field_fields[title]'] = 'title';
$edit['unique_field_scope'] = UNIQUE_FIELD_SCOPE_ALL;
$this->drupalPost('admin/structure/types/add', $edit, t('Save content type'));
$this->assertText('The content type Unique Title has been added.', 'Content type added.');
// Create another content type with the title set to be unique
$edit = array();
$edit['name'] = 'Unique Title 2';
$edit['type'] = 'uf_title2';
$edit['unique_field_fields[title]'] = 'title';
$edit['unique_field_scope'] = UNIQUE_FIELD_SCOPE_ALL;
$this->drupalPost('admin/structure/types/add', $edit, t('Save content type'));
$this->assertText('The content type Unique Title 2 has been added.', 'Content type added.');
// Attempt to create 2 nodes with the same title in different content types
$title = $this->randomName(24);
$edit = array();
$edit['title'] = $title;
$edit['body[und][0][value]'] = $this->randomName(48);
$this->drupalPost('node/add/uf-title', $edit, t('Save'));
$this->assertText($edit['body[und][0][value]'], 'Unique Title (uf_title) node has been created');
$edit['body[und][0][value]'] = $this->randomName(48);
$this->drupalPost('node/add/uf-title2', $edit, t('Save'));
$this->assertText('The Title field requires a unique value, and the specified value is already used', 'Unique Title (uf_title) node with duplicate content could not be created');
// Check for false negative: Attempt to create a node with a unique title
$edit = array();
$edit['title'] = $this->randomName(24);
$edit['body[und][0][value]'] = $this->randomName(48);
$this->drupalPost('node/add/uf-title2', $edit, t('Save'));
$this->assertText($edit['body[und][0][value]'], 'Unique Title 2 (uf_title2) node has been created');
}
/**
* Test the unique requirement on the node author in the content type scope.
*/
public function testUniqueCtypeAuthor() {
// Create a content type with the author set to be unique
$edit = array();
$edit['name'] = 'Unique Author';
$edit['type'] = 'uf_author';
$edit['unique_field_fields[name]'] = 'name';
$this->drupalPost('admin/structure/types/add', $edit, t('Save content type'));
$this->assertText('The content type Unique Author has been added.', 'Content type added.');
// Attempt to create 2 nodes with the same author
$edit = array();
$edit['title'] = $this->randomName(24);
$edit['body[und][0][value]'] = $this->randomName(48);
$this->drupalPost('node/add/uf-author', $edit, t('Save'));
$this->assertText($edit['body[und][0][value]'], 'Unique Author (uf_author) node has been created');
$edit = array();
$edit['title'] = $this->randomName(24);
$edit['body[und][0][value]'] = $this->randomName(48);
$this->drupalPost('node/add/uf-author', $edit, t('Save'));
$this->assertText('The Author field requires a unique value, and the specified value is already used', 'Unique Author (uf_author) node with duplicate content could not be created');
// Check for false negative: Attempt to create a node with a unique author
$new_account = $this->drupalCreateUser(array('administer nodes', 'bypass node access'));
$this->drupalLogin($new_account);
$edit = array();
$edit['title'] = $this->randomName(24);
$edit['body[und][0][value]'] = $this->randomName(48);
$this->drupalPost('node/add/uf-author', $edit, t('Save'));
$this->assertText($edit['body[und][0][value]'], 'Unique Author (uf_author) node has been created');
}
/**
* Test the unique requirement on the node author in the all scope.
*/
public function testUniqueAllAuthor() {
// Create a content type with the author set to be unique
$edit = array();
$edit['name'] = 'Unique Author';
$edit['type'] = 'uf_author';
$edit['unique_field_fields[name]'] = 'name';
$edit['unique_field_scope'] = UNIQUE_FIELD_SCOPE_ALL;
$this->drupalPost('admin/structure/types/add', $edit, t('Save content type'));
$this->assertText('The content type Unique Author has been added.', 'Content type added.');
// Create another content type with the author set to be unique
$edit = array();
$edit['name'] = 'Unique Author 2';
$edit['type'] = 'uf_author2';
$edit['unique_field_fields[name]'] = 'name';
$edit['unique_field_scope'] = UNIQUE_FIELD_SCOPE_ALL;
$this->drupalPost('admin/structure/types/add', $edit, t('Save content type'));
$this->assertText('The content type Unique Author 2 has been added.', 'Content type added.');
// Attempt to create 2 nodes with the same author in different content types
$edit = array();
$edit['title'] = $this->randomName(24);
$edit['body[und][0][value]'] = $this->randomName(48);
$this->drupalPost('node/add/uf-author', $edit, t('Save'));
$this->assertText($edit['body[und][0][value]'], 'Unique Author (uf_author) node has been created');
$edit = array();
$edit['title'] = $this->randomName(24);
$edit['body[und][0][value]'] = $this->randomName(48);
$this->drupalPost('node/add/uf-author2', $edit, t('Save'));
$this->assertText('The Author field requires a unique value, and the specified value is already used', 'Unique Author 2 (uf_author2) node with duplicate content could not be created');
// Check for false negative: Attempt to create a node with a unique title
$new_account = $this->drupalCreateUser(array('administer nodes', 'bypass node access'));
$this->drupalLogin($new_account);
$edit = array();
$edit['title'] = $this->randomName(24);
$edit['body[und][0][value]'] = $this->randomName(48);
$this->drupalPost('node/add/uf-author2', $edit, t('Save'));
$this->assertText($edit['body[und][0][value]'], 'Unique Author 2 (uf_author2) node has been created');
}
/**
* Test the unique requirement on a text field in the content type scope.
*/
public function testUniqueCtypeText() {
// Create a content type with a text field that is set to be unique
$edit = array();
$edit['name'] = 'Unique Text';
$edit['type'] = 'uf_text';
$this->drupalPost('admin/structure/types/add', $edit, t('Save content type'));
$this->assertText('The content type Unique Text has been added.', 'Content type added.');
$edit = array();
$edit['fields[_add_new_field][label]'] = 'Unique Text Text';
$edit['fields[_add_new_field][field_name]'] = 'uf_text_text';
$edit['fields[_add_new_field][type]'] = 'text';
$edit['fields[_add_new_field][widget_type]'] = 'text_textfield';
$this->drupalPost('admin/structure/types/manage/uf_text/fields', $edit, t('Save'));
$this->assertText('These settings apply to the Unique Text Text field everywhere it is used.', 'Field added to content type.');
$edit = array();
$edit['unique_field_fields[field_uf_text_text]'] = 'field_uf_text_text';
$this->drupalPost('admin/structure/types/manage/uf_text', $edit, t('Save content type'));
$this->assertText('The content type Unique Text has been updated.', 'Content type updated.');
// Attempt to create 2 nodes with the same text field value
$text = $this->randomName(48);
$edit = array();
$edit['title'] = $this->randomName(24);
$edit['body[und][0][value]'] = $this->randomName(48);
$edit['field_uf_text_text[und][0][value]'] = $text;
$this->drupalPost('node/add/uf-text', $edit, t('Save'));
$this->assertText($edit['body[und][0][value]'], 'Unique Text (uf_text) node has been created');
$edit['body[und][0][value]'] = $this->randomName(48);
$this->drupalPost('node/add/uf-text', $edit, t('Save'));
$this->assertText('The Unique Text Text field requires a unique value, and the specified value is already used', 'Unique Text (uf_text) node with duplicate content could not be created');
// Check for false negative: Attempt to create a node with a unique text
$edit = array();
$edit['title'] = $this->randomName(24);
$edit['body[und][0][value]'] = $this->randomName(48);
$edit['field_uf_text_text[und][0][value]'] = $this->randomName(48);
$this->drupalPost('node/add/uf-text', $edit, t('Save'));
$this->assertText($edit['body[und][0][value]'], 'Unique Text (uf_text) node has been created');
}
/**
* Test the unique requirement on a text field in the all scope.
*/
public function testUniqueAllText() {
// Create a content type with a text field that is set to be unique
$edit = array();
$edit['name'] = 'Unique Text';
$edit['type'] = 'uf_text';
$this->drupalPost('admin/structure/types/add', $edit, t('Save content type'));
$this->assertText('The content type Unique Text has been added.', 'Content type added.');
$edit = array();
$edit['fields[_add_new_field][label]'] = 'Unique Text Text';
$edit['fields[_add_new_field][field_name]'] = 'uf_text_text';
$edit['fields[_add_new_field][type]'] = 'text';
$edit['fields[_add_new_field][widget_type]'] = 'text_textfield';
$this->drupalPost('admin/structure/types/manage/uf_text/fields', $edit, t('Save'));
$this->assertText('These settings apply to the Unique Text Text field everywhere it is used.', 'Field added to content type.');
$edit = array();
$edit['unique_field_scope'] = UNIQUE_FIELD_SCOPE_ALL;
$edit['unique_field_fields[field_uf_text_text]'] = 'field_uf_text_text';
$this->drupalPost('admin/structure/types/manage/uf_text', $edit, t('Save content type'));
$this->assertText('The content type Unique Text has been updated.', 'Content type updated.');
// Create another content type with the same text field and set it to unique
$edit = array();
$edit['name'] = 'Unique Text 2';
$edit['type'] = 'uf_text2';
$this->drupalPost('admin/structure/types/add', $edit, t('Save content type'));
$this->assertText('The content type Unique Text 2 has been added.', 'Content type added.');
$edit = array();
$edit['fields[_add_existing_field][label]'] = 'Unique Text 2 Text';
$edit['fields[_add_existing_field][field_name]'] = 'field_uf_text_text';
$edit['fields[_add_existing_field][widget_type]'] = 'text_textfield';
$this->drupalPost('admin/structure/types/manage/uf_text2/fields', $edit, t('Save'));
$this->assertText('These settings apply only to the Unique Text 2 Text field when used in the Unique Text 2 type.', 'Field added to content type.');
$edit = array();
$edit['unique_field_scope'] = UNIQUE_FIELD_SCOPE_ALL;
$edit['unique_field_fields[field_uf_text_text]'] = 'field_uf_text_text';
$this->drupalPost('admin/structure/types/manage/uf_text2', $edit, t('Save content type'));
$this->assertText('The content type Unique Text 2 has been updated.', 'Content type updated.');
// Attempt to create 2 nodes with the same text in different content types
$text = $this->randomName(48);
$edit = array();
$edit['title'] = $this->randomName(24);
$edit['body[und][0][value]'] = $this->randomName(48);
$edit['field_uf_text_text[und][0][value]'] = $text;
$this->drupalPost('node/add/uf-text', $edit, t('Save'));
$this->assertText($edit['body[und][0][value]'], 'Unique Text (uf_text) node has been created');
$edit = array();
$edit['title'] = $this->randomName(24);
$edit['body[und][0][value]'] = $this->randomName(48);
$edit['field_uf_text_text[und][0][value]'] = $text;
$this->drupalPost('node/add/uf-text2', $edit, t('Save'));
$this->assertText('The Unique Text 2 Text field requires a unique value, and the specified value is already used', 'Unique Text 2 (uf_text2) node with duplicate content could not be created');
// Check for false negative: Attempt to create a node with a unique text
$edit = array();
$edit['title'] = $this->randomName(24);
$edit['body[und][0][value]'] = $this->randomName(48);
$edit['field_uf_text_text[und][0][value]'] = $this->randomName(48);
$this->drupalPost('node/add/uf-text2', $edit, t('Save'));
$this->assertText($edit['body[und][0][value]'], 'Unique Text 2 (uf_text2) node has been created');
}
/**
* Test the unique requirement on a text field in the single scope.
*/
public function testUniqueSingleText() {
// Create a content type with two text fields that are set to be unique
$edit = array();
$edit['name'] = 'Unique Text Single';
$edit['type'] = 'uf_text_single';
$this->drupalPost('admin/structure/types/add', $edit, t('Save content type'));
$this->assertText('The content type Unique Text Single has been added.', 'Content type added.');
$edit = array();
$edit['fields[_add_new_field][label]'] = 'Unique Text Single Text 1';
$edit['fields[_add_new_field][field_name]'] = 'uf_text_single_text_1';
$edit['fields[_add_new_field][type]'] = 'text';
$edit['fields[_add_new_field][widget_type]'] = 'text_textfield';
$this->drupalPost('admin/structure/types/manage/uf_text_single/fields', $edit, t('Save'));
$this->assertText('These settings apply to the Unique Text Single Text 1 field everywhere it is used.', 'Field added to content type.');
$edit = array();
$edit['fields[_add_new_field][label]'] = 'Unique Text Single Text 2';
$edit['fields[_add_new_field][field_name]'] = 'uf_text_single_text_2';
$edit['fields[_add_new_field][type]'] = 'text';
$edit['fields[_add_new_field][widget_type]'] = 'text_textfield';
$this->drupalPost('admin/structure/types/manage/uf_text_single/fields', $edit, t('Save'));
$this->assertText('These settings apply to the Unique Text Single Text 2 field everywhere it is used.', 'Field added to content type.');
$edit = array();
$edit['unique_field_scope'] = UNIQUE_FIELD_SCOPE_NODE;
$edit['unique_field_fields[field_uf_text_single_text_1]'] = 'field_uf_text_single_text_1';
$edit['unique_field_fields[field_uf_text_single_text_2]'] = 'field_uf_text_single_text_2';
$this->drupalPost('admin/structure/types/manage/uf_text_single', $edit, t('Save content type'));
$this->assertText('The content type Unique Text Single has been updated.', 'Content type updated.');
// Attempt to create a node with the same text in both fields
$text = $this->randomName(48);
$edit = array();
$edit['title'] = $this->randomName(24);
$edit['body[und][0][value]'] = $this->randomName(48);
$edit['field_uf_text_single_text_1[und][0][value]'] = $text;
$edit['field_uf_text_single_text_2[und][0][value]'] = $text;
$this->drupalPost('node/add/uf-text-single', $edit, t('Save'));
$this->assertText('The Unique Text Single Text 2 fields must have unique values. The Unique Text Single Text 2 field has a value that is already used.', 'Unique Text Single (uf_text_single) node with duplicate content could not be created');
// Check for false negative: Attempt to create a node with unique text
$edit = array();
$edit['title'] = $this->randomName(24);
$edit['body[und][0][value]'] = $this->randomName(48);
$edit['field_uf_text_single_text_1[und][0][value]'] = $this->randomName(48);
$edit['field_uf_text_single_text_2[und][0][value]'] = $this->randomName(48);
$this->drupalPost('node/add/uf-text-single', $edit, t('Save'));
$this->assertText($edit['body[und][0][value]'], 'Unique Text Single (uf_text_single) node has been created');
}
/**
* Test the unique requirement on an integer field in the content type scope.
*/
public function testUniqueCtypeInteger() {
// Create a content type with an integer field that is set to be unique
$edit = array();
$edit['name'] = 'Unique Integer';
$edit['type'] = 'uf_int';
$this->drupalPost('admin/structure/types/add', $edit, t('Save content type'));
$this->assertText('The content type Unique Integer has been added.', 'Content type added.');
$edit = array();
$edit['fields[_add_new_field][label]'] = 'Unique Integer Integer';
$edit['fields[_add_new_field][field_name]'] = 'uf_int_int';
$edit['fields[_add_new_field][type]'] = 'number_integer';
$edit['fields[_add_new_field][widget_type]'] = 'number';
$this->drupalPost('admin/structure/types/manage/uf_int/fields', $edit, t('Save'));
$this->assertText('These settings apply to the Unique Integer Integer field everywhere it is used.', 'Field added to content type.');
$edit = array();
$edit['unique_field_fields[field_uf_int_int]'] = 'field_uf_int_int';
$this->drupalPost('admin/structure/types/manage/uf_int', $edit, t('Save content type'));
$this->assertText('The content type Unique Integer has been updated.', 'Content type updated.');
// Attempt to create 2 nodes with the same integer value
$num = mt_rand();
$edit = array();
$edit['title'] = $this->randomName(24);
$edit['body[und][0][value]'] = $this->randomName(48);
$edit['field_uf_int_int[und][0][value]'] = $num;
$this->drupalPost('node/add/uf-int', $edit, t('Save'));
$this->assertText($edit['body[und][0][value]'], 'Unique Integer (uf_int) node has been created');
$edit['body[und][0][value]'] = $this->randomName(48);
$this->drupalPost('node/add/uf-int', $edit, t('Save'));
$this->assertText('The Unique Integer Integer field requires a unique value, and the specified value is already used', 'Unique Integer (uf_int) node with duplicate content could not be created');
// Check for false negative: Attempt to create a node with a unique integer
$edit = array();
$edit['title'] = $this->randomName(24);
$edit['body[und][0][value]'] = $this->randomName(48);
$edit['field_uf_int_int[und][0][value]'] = $num - 1;
$this->drupalPost('node/add/uf-int', $edit, t('Save'));
$this->assertText($edit['body[und][0][value]'], 'Unique Integer (uf_int) node has been created');
}
/**
* Test the unique requirement on an integer field in the all scope.
*/
public function testUniqueAllInteger() {
// Create a content type with an integer field that is set to be unique
$edit = array();
$edit['name'] = 'Unique Integer';
$edit['type'] = 'uf_int';
$this->drupalPost('admin/structure/types/add', $edit, t('Save content type'));
$this->assertText('The content type Unique Integer has been added.', 'Content type added.');
$edit = array();
$edit['fields[_add_new_field][label]'] = 'Unique Integer Integer';
$edit['fields[_add_new_field][field_name]'] = 'uf_int_int';
$edit['fields[_add_new_field][type]'] = 'number_integer';
$edit['fields[_add_new_field][widget_type]'] = 'number';
$this->drupalPost('admin/structure/types/manage/uf_int/fields', $edit, t('Save'));
$this->assertText('These settings apply to the Unique Integer Integer field everywhere it is used.', 'Field added to content type.');
$edit = array();
$edit['unique_field_scope'] = UNIQUE_FIELD_SCOPE_ALL;
$edit['unique_field_fields[field_uf_int_int]'] = 'field_uf_int_int';
$this->drupalPost('admin/structure/types/manage/uf_int', $edit, t('Save content type'));
$this->assertText('The content type Unique Integer has been updated.', 'Content type updated.');
// Create another content type with the same integer field and set it to unique
$edit = array();
$edit['name'] = 'Unique Integer 2';
$edit['type'] = 'uf_int2';
$this->drupalPost('admin/structure/types/add', $edit, t('Save content type'));
$this->assertText('The content type Unique Integer 2 has been added.', 'Content type added.');
$edit = array();
$edit['fields[_add_existing_field][label]'] = 'Unique Integer 2 Integer';
$edit['fields[_add_existing_field][field_name]'] = 'field_uf_int_int';
$edit['fields[_add_existing_field][widget_type]'] = 'number';
$this->drupalPost('admin/structure/types/manage/uf_int2/fields', $edit, t('Save'));
$this->assertText('These settings apply only to the Unique Integer 2 Integer field when used in the Unique Integer 2 type.', 'Field added to content type.');
$edit = array();
$edit['unique_field_scope'] = UNIQUE_FIELD_SCOPE_ALL;
$edit['unique_field_fields[field_uf_int_int]'] = 'field_uf_int_int';
$this->drupalPost('admin/structure/types/manage/uf_int2', $edit, t('Save content type'));
$this->assertText('The content type Unique Integer 2 has been updated.', 'Content type updated.');
// Attempt to create 2 nodes with the same integer in different content types
$num = mt_rand();
$edit = array();
$edit['title'] = $this->randomName(24);
$edit['body[und][0][value]'] = $this->randomName(48);
$edit['field_uf_int_int[und][0][value]'] = $num;
$this->drupalPost('node/add/uf-int', $edit, t('Save'));
$this->assertText($edit['body[und][0][value]'], 'Unique Integer (uf_int) node has been created');
$edit = array();
$edit['title'] = $this->randomName(24);
$edit['body[und][0][value]'] = $this->randomName(48);
$edit['field_uf_int_int[und][0][value]'] = $num;
$this->drupalPost('node/add/uf-int2', $edit, t('Save'));
$this->assertText('The Unique Integer 2 Integer field requires a unique value, and the specified value is already used', 'Unique Integer 2 (uf_int2) node with duplicate content could not be created');
// Check for false negative: Attempt to create a node with a unique text
$edit = array();
$edit['title'] = $this->randomName(24);
$edit['body[und][0][value]'] = $this->randomName(48);
$edit['field_uf_int_int[und][0][value]'] = $num - 1;
$this->drupalPost('node/add/uf-int2', $edit, t('Save'));
$this->assertText($edit['body[und][0][value]'], 'Unique Integer 2 (uf_int2) node has been created');
}
/**
* Test the unique requirement on an integer field in the single scope.
*/
public function testUniqueSingleInteger() {
// Create a content type with two integer fields that are set to be unique
$edit = array();
$edit['name'] = 'Unique Integer Single';
$edit['type'] = 'uf_int_single';
$this->drupalPost('admin/structure/types/add', $edit, t('Save content type'));
$this->assertText('The content type Unique Integer Single has been added.', 'Content type added.');
$edit = array();
$edit['fields[_add_new_field][label]'] = 'Unique Integer Single Integer 1';
$edit['fields[_add_new_field][field_name]'] = 'uf_int_single_int_1';
$edit['fields[_add_new_field][type]'] = 'number_integer';
$edit['fields[_add_new_field][widget_type]'] = 'number';
$this->drupalPost('admin/structure/types/manage/uf_int_single/fields', $edit, t('Save'));
$this->assertText('These settings apply to the Unique Integer Single Integer 1 field everywhere it is used.', 'Field added to content type.');
$edit = array();
$edit['fields[_add_new_field][label]'] = 'Unique Integer Single Integer 2';
$edit['fields[_add_new_field][field_name]'] = 'uf_int_single_int_2';
$edit['fields[_add_new_field][type]'] = 'number_integer';
$edit['fields[_add_new_field][widget_type]'] = 'number';
$this->drupalPost('admin/structure/types/manage/uf_int_single/fields', $edit, t('Save'));
$this->assertText('These settings apply to the Unique Integer Single Integer 2 field everywhere it is used.', 'Field added to content type.');
$edit = array();
$edit['unique_field_scope'] = UNIQUE_FIELD_SCOPE_NODE;
$edit['unique_field_fields[field_uf_int_single_int_1]'] = 'field_uf_int_single_int_1';
$edit['unique_field_fields[field_uf_int_single_int_2]'] = 'field_uf_int_single_int_2';
$this->drupalPost('admin/structure/types/manage/uf_int_single', $edit, t('Save content type'));
$this->assertText('The content type Unique Integer Single has been updated.', 'Content type updated.');
// Attempt to create a node with the same integer in both fields
$num = mt_rand();
$edit = array();
$edit['title'] = $this->randomName(24);
$edit['body[und][0][value]'] = $this->randomName(48);
$edit['field_uf_int_single_int_1[und][0][value]'] = $num;
$edit['field_uf_int_single_int_2[und][0][value]'] = $num;
$this->drupalPost('node/add/uf-int-single', $edit, t('Save'));
$this->assertText('The Unique Integer Single Integer 2 fields must have unique values. The Unique Integer Single Integer 2 field has a value that is already used.', 'Unique Integer Single (uf_int_single) node with duplicate content could not be created');
// Check for false negative: Attempt to create a node with unique numbers
$edit = array();
$edit['title'] = $this->randomName(24);
$edit['body[und][0][value]'] = $this->randomName(48);
$edit['field_uf_int_single_int_1[und][0][value]'] = $num;
$edit['field_uf_int_single_int_2[und][0][value]'] = $num - 1;
$this->drupalPost('node/add/uf-int-single', $edit, t('Save'));
$this->assertText($edit['body[und][0][value]'], 'Unique Integer Single (uf_int_single) node has been created');
}
}

View File

@@ -0,0 +1,227 @@
<?php
/**
* @file
* Functional tests for the Unique Field module with Date field types.
*/
class UniqueFieldDateTestCase extends DrupalWebTestCase {
protected $privileged_user;
public static function getInfo() {
return array(
'name' => 'Unique Field: Date module tests',
'description' => 'Ensure that the Unique Field module functions properly with Date field types.',
'group' => 'Unique Field',
);
}
public function setUp() {
parent::setUp('field', 'field_ui', 'options', 'date', 'unique_field');
// Create and log in our privileged user.
$this->privileged_user = $this->drupalCreateUser(array(
'administer content types', 'administer nodes', 'bypass node access', 'unique_field_perm_admin', 'unique_field_perm_bypass',
));
$this->drupalLogin($this->privileged_user);
}
/**
* Test the unique requirement on a date field in the content type scope.
*/
public function testUniqueCtypeNode() {
// Create a content type with a node reference field that is set to be
// unique
$edit = array();
$edit['name'] = 'Unique Node';
$edit['type'] = 'uf_node';
$this->drupalPost('admin/structure/types/add', $edit, t('Save content type'));
$this->assertText('The content type Unique Node has been added.', 'Content type added.');
$edit = array();
$edit['fields[_add_new_field][label]'] = 'Unique Node Date';
$edit['fields[_add_new_field][field_name]'] = 'uf_node_date';
$edit['fields[_add_new_field][type]'] = 'date';
$edit['fields[_add_new_field][widget_type]'] = 'date_select';
$this->drupalPost('admin/structure/types/manage/uf_node/fields', $edit, t('Save'));
$this->assertText('These settings apply to the Unique Node Date field everywhere it is used.', 'Field added to content type.');
$edit = array();
$edit['unique_field_fields[field_uf_node_date]'] = 'field_uf_node_date';
$this->drupalPost('admin/structure/types/manage/uf_node', $edit, t('Save content type'));
$this->assertText('The content type Unique Node has been updated.', 'Content type updated.');
// Attempt to create 2 nodes with the same date value
$date = new DateTime('2011-07-11 11:11:11');
$edit = array();
$edit['title'] = $this->randomName(24);
$edit['body[und][0][value]'] = $this->randomName(48);
$edit['field_uf_node_date[und][0][value][month]'] = $date->format('n');
$edit['field_uf_node_date[und][0][value][day]'] = $date->format('j');
$edit['field_uf_node_date[und][0][value][year]'] = $date->format('Y');
$edit['field_uf_node_date[und][0][value][hour]'] = $date->format('G');
$edit['field_uf_node_date[und][0][value][minute]'] = $date->format('i');
$this->drupalPost('node/add/uf-node', $edit, t('Save'));
$this->assertText($edit['body[und][0][value]'], 'Unique Node (uf_node) node has been created');
$edit['title'] = $this->randomName(24);
$edit['body[und][0][value]'] = $this->randomName(48);
$this->drupalPost('node/add/uf-node', $edit, t('Save'));
$this->assertText('The Unique Node Date field requires a unique value, and the specified value is already used', 'Unique Node (uf_node) node with duplicate content could not be created');
// Check for false negative: Attempt to create a node with a unique date
$date = new DateTime('2010-02-02 17:43:11');
$edit = array();
$edit['title'] = $this->randomName(24);
$edit['body[und][0][value]'] = $this->randomName(48);
$edit['field_uf_node_date[und][0][value][month]'] = $date->format('n');
$edit['field_uf_node_date[und][0][value][day]'] = $date->format('j');
$edit['field_uf_node_date[und][0][value][year]'] = $date->format('Y');
$edit['field_uf_node_date[und][0][value][hour]'] = $date->format('G');
$edit['field_uf_node_date[und][0][value][minute]'] = $date->format('i');
$this->drupalPost('node/add/uf-node', $edit, t('Save'));
$this->assertText($edit['body[und][0][value]'], 'Unique Node (uf_node) node has been created');
}
/**
* Test the unique requirement on a date field in the all scope.
*/
public function testUniqueAllNode() {
// Create a content type with a node reference field that is set to be
// unique
$edit = array();
$edit['name'] = 'Unique Node';
$edit['type'] = 'uf_node';
$this->drupalPost('admin/structure/types/add', $edit, t('Save content type'));
$this->assertText('The content type Unique Node has been added.', 'Content type added.');
$edit = array();
$edit['fields[_add_new_field][label]'] = 'Unique Node Date';
$edit['fields[_add_new_field][field_name]'] = 'uf_node_date';
$edit['fields[_add_new_field][type]'] = 'date';
$edit['fields[_add_new_field][widget_type]'] = 'date_select';
$this->drupalPost('admin/structure/types/manage/uf_node/fields', $edit, t('Save'));
$this->assertText('These settings apply to the Unique Node Date field everywhere it is used.', 'Field added to content type.');
$edit = array();
$edit['unique_field_fields[field_uf_node_date]'] = 'field_uf_node_date';
$edit['unique_field_scope'] = UNIQUE_FIELD_SCOPE_ALL;
$this->drupalPost('admin/structure/types/manage/uf_node', $edit, t('Save content type'));
$this->assertText('The content type Unique Node has been updated.', 'Content type updated.');
// Create another content type with a node reference field that is set to be
// unique
$edit = array();
$edit['name'] = 'Unique Node 2';
$edit['type'] = 'uf_node2';
$this->drupalPost('admin/structure/types/add', $edit, t('Save content type'));
$this->assertText('The content type Unique Node 2 has been added.', 'Content type added.');
$edit = array();
$edit['fields[_add_existing_field][label]'] = 'Unique Node 2 Date';
$edit['fields[_add_existing_field][field_name]'] = 'field_uf_node_date';
$edit['fields[_add_existing_field][widget_type]'] = 'date_select';
$this->drupalPost('admin/structure/types/manage/uf_node2/fields', $edit, t('Save'));
$this->assertText('These settings apply only to the Unique Node 2 Date field when used in the Unique Node 2 type.', 'Field added to content type.');
$edit = array();
$edit['unique_field_fields[field_uf_node_date]'] = 'field_uf_node_date';
$edit['unique_field_scope'] = UNIQUE_FIELD_SCOPE_ALL;
$this->drupalPost('admin/structure/types/manage/uf_node2', $edit, t('Save content type'));
$this->assertText('The content type Unique Node 2 has been updated.', 'Content type updated.');
// Attempt to create 2 nodes with the same date value in different content
// types
$date = new DateTime('2011-03-28 04:27:33');
$edit = array();
$edit['title'] = $this->randomName(24);
$edit['body[und][0][value]'] = $this->randomName(48);
$edit['field_uf_node_date[und][0][value][month]'] = $date->format('n');
$edit['field_uf_node_date[und][0][value][day]'] = $date->format('j');
$edit['field_uf_node_date[und][0][value][year]'] = $date->format('Y');
$edit['field_uf_node_date[und][0][value][hour]'] = $date->format('G');
$edit['field_uf_node_date[und][0][value][minute]'] = $date->format('i');
$this->drupalPost('node/add/uf-node', $edit, t('Save'));
$this->assertText($edit['body[und][0][value]'], 'Unique Node (uf_node) node has been created');
$edit['title'] = $this->randomName(24);
$edit['body[und][0][value]'] = $this->randomName(48);
$this->drupalPost('node/add/uf-node2', $edit, t('Save'));
$this->assertText('The Unique Node 2 Date field requires a unique value, and the specified value is already used', 'Unique Node 2 (uf_node2) node with duplicate content could not be created');
// Check for false negative: Attempt to create a node with a unique date
$date = new DateTime('2010-11-19 13:23:31');
$edit = array();
$edit['title'] = $this->randomName(24);
$edit['body[und][0][value]'] = $this->randomName(48);
$edit['field_uf_node_date[und][0][value][month]'] = $date->format('n');
$edit['field_uf_node_date[und][0][value][day]'] = $date->format('j');
$edit['field_uf_node_date[und][0][value][year]'] = $date->format('Y');
$edit['field_uf_node_date[und][0][value][hour]'] = $date->format('G');
$edit['field_uf_node_date[und][0][value][minute]'] = $date->format('i');
$this->drupalPost('node/add/uf-node2', $edit, t('Save'));
$this->assertText($edit['body[und][0][value]'], 'Unique Node 2 (uf_node2) node has been created');
}
/**
* Test the unique requirement on a date field in the single scope.
*/
public function testUniqueSingleNode() {
// Create a content type with two date fields that are set to be unique
$edit = array();
$edit['name'] = 'Unique Node Single';
$edit['type'] = 'uf_node_single';
$this->drupalPost('admin/structure/types/add', $edit, t('Save content type'));
$this->assertText('The content type Unique Node Single has been added.', 'Content type added.');
$edit = array();
$edit['fields[_add_new_field][label]'] = 'Unique Node Single Date 1';
$edit['fields[_add_new_field][field_name]'] = 'uf_node_single_date_1';
$edit['fields[_add_new_field][type]'] = 'date';
$edit['fields[_add_new_field][widget_type]'] = 'date_select';
$this->drupalPost('admin/structure/types/manage/uf_node_single/fields', $edit, t('Save'));
$this->assertText('These settings apply to the Unique Node Single Date 1 field everywhere it is used.', 'Field added to content type.');
$edit = array();
$edit['fields[_add_new_field][label]'] = 'Unique Node Single Date 2';
$edit['fields[_add_new_field][field_name]'] = 'uf_node_single_date_2';
$edit['fields[_add_new_field][type]'] = 'date';
$edit['fields[_add_new_field][widget_type]'] = 'date_select';
$this->drupalPost('admin/structure/types/manage/uf_node_single/fields', $edit, t('Save'));
$this->assertText('These settings apply to the Unique Node Single Date 2 field everywhere it is used.', 'Field added to content type.');
$edit = array();
$edit['unique_field_scope'] = UNIQUE_FIELD_SCOPE_NODE;
$edit['unique_field_fields[field_uf_node_single_date_1]'] = 'field_uf_node_single_date_1';
$edit['unique_field_fields[field_uf_node_single_date_2]'] = 'field_uf_node_single_date_2';
$this->drupalPost('admin/structure/types/manage/uf_node_single', $edit, t('Save content type'));
$this->assertText('The content type Unique Node Single has been updated.', 'Content type updated.');
// Attempt to create a node with the same date in both fields
$date = new DateTime('2011-06-12 19:08:22');
$edit = array();
$edit['title'] = $this->randomName(24);
$edit['body[und][0][value]'] = $this->randomName(48);
$edit['field_uf_node_single_date_1[und][0][value][month]'] = $date->format('n');
$edit['field_uf_node_single_date_1[und][0][value][day]'] = $date->format('j');
$edit['field_uf_node_single_date_1[und][0][value][year]'] = $date->format('Y');
$edit['field_uf_node_single_date_1[und][0][value][hour]'] = $date->format('G');
$edit['field_uf_node_single_date_1[und][0][value][minute]'] = $date->format('i');
$edit['field_uf_node_single_date_2[und][0][value][month]'] = $date->format('n');
$edit['field_uf_node_single_date_2[und][0][value][day]'] = $date->format('j');
$edit['field_uf_node_single_date_2[und][0][value][year]'] = $date->format('Y');
$edit['field_uf_node_single_date_2[und][0][value][hour]'] = $date->format('G');
$edit['field_uf_node_single_date_2[und][0][value][minute]'] = $date->format('i');
$this->drupalPost('node/add/uf-node-single', $edit, t('Save'));
$this->assertText('The Unique Node Single Date 2 fields must have unique values. The Unique Node Single Date 2 field has a value that is already used.', 'Unique Node Single (uf_node_single) node with duplicate content could not be created');
// Check for false negative: Attempt to create a node with unique text
$date1 = new DateTime('2011-01-12 19:08:22');
$date2 = new DateTime('2011-04-12 19:08:22');
$edit = array();
$edit['title'] = $this->randomName(24);
$edit['body[und][0][value]'] = $this->randomName(48);
$edit['field_uf_node_single_date_1[und][0][value][month]'] = $date1->format('n');
$edit['field_uf_node_single_date_1[und][0][value][day]'] = $date1->format('j');
$edit['field_uf_node_single_date_1[und][0][value][year]'] = $date1->format('Y');
$edit['field_uf_node_single_date_1[und][0][value][hour]'] = $date1->format('G');
$edit['field_uf_node_single_date_1[und][0][value][minute]'] = $date1->format('i');
$edit['field_uf_node_single_date_2[und][0][value][month]'] = $date2->format('n');
$edit['field_uf_node_single_date_2[und][0][value][day]'] = $date2->format('j');
$edit['field_uf_node_single_date_2[und][0][value][year]'] = $date2->format('Y');
$edit['field_uf_node_single_date_2[und][0][value][hour]'] = $date2->format('G');
$edit['field_uf_node_single_date_2[und][0][value][minute]'] = $date2->format('i');
$this->drupalPost('node/add/uf-node-single', $edit, t('Save'));
$this->assertText($edit['body[und][0][value]'], 'Unique Node Single (uf_node_single) node has been created');
}
}

View File

@@ -0,0 +1,238 @@
<?php
/**
* @file
* Functional tests for the Unique Field module with References field types.
*/
class UniqueFieldReferencesTestCase extends DrupalWebTestCase {
protected $privileged_user;
public static function getInfo() {
return array(
'name' => 'Unique Field: References module tests',
'description' => 'Ensure that the Unique Field module functions properly with References field types.',
'group' => 'Unique Field',
);
}
public function setUp() {
parent::setUp('field', 'field_ui', 'options', 'node_reference', 'user_reference', 'unique_field');
// Create and log in our privileged user.
$this->privileged_user = $this->drupalCreateUser(array(
'administer content types', 'administer nodes', 'bypass node access', 'unique_field_perm_admin', 'unique_field_perm_bypass',
));
$this->drupalLogin($this->privileged_user);
}
/**
* Test the unique requirement on a node reference field in the content type
* scope.
*/
public function testUniqueCtypeNode() {
// Create a content type with a node reference field that is set to be
// unique
$edit = array();
$edit['name'] = 'Unique Node';
$edit['type'] = 'uf_node';
$this->drupalPost('admin/structure/types/add', $edit, t('Save content type'));
$this->assertText('The content type Unique Node has been added.', 'Content type added.');
$edit = array();
$edit['fields[_add_new_field][label]'] = 'Unique Node Node';
$edit['fields[_add_new_field][field_name]'] = 'uf_node_node';
$edit['fields[_add_new_field][type]'] = 'node_reference';
$edit['fields[_add_new_field][widget_type]'] = 'options_select';
$this->drupalPost('admin/structure/types/manage/uf_node/fields', $edit, t('Save'));
$this->assertText('These settings apply to the Unique Node Node field everywhere it is used.', 'Field added to content type.');
$edit = array();
$edit['field[settings][referenceable_types][uf_node]'] = 'uf_node';
$this->drupalPost('admin/structure/types/manage/uf_node/fields/field_uf_node_node/field-settings', $edit, t('Save field settings'));
$this->assertText('Updated field Unique Node Node field settings.', 'Node reference field configured.');
$edit = array();
$edit['unique_field_fields[field_uf_node_node]'] = 'field_uf_node_node';
$this->drupalPost('admin/structure/types/manage/uf_node', $edit, t('Save content type'));
$this->assertText('The content type Unique Node has been updated.', 'Content type updated.');
// Create a node to be referenced
$edit = array();
$edit['title'] = $this->randomName(24);
$edit['body[und][0][value]'] = $this->randomName(48);
$this->drupalPost('node/add/uf-node', $edit, t('Save'));
$this->assertText($edit['body[und][0][value]'], 'Unique Node (uf_node) node has been created');
// Attempt to create 2 nodes that reference the same node
$ref_nid = 1;
$edit = array();
$edit['title'] = $this->randomName(24);
$edit['body[und][0][value]'] = $this->randomName(48);
$edit['field_uf_node_node[und]'] = $ref_nid;
$this->drupalPost('node/add/uf-node', $edit, t('Save'));
$this->assertText($edit['body[und][0][value]'], 'Unique Node (uf_node) node has been created');
$edit['body[und][0][value]'] = $this->randomName(48);
$this->drupalPost('node/add/uf-node', $edit, t('Save'));
$this->assertText('The Unique Node Node field requires a unique value, and the specified value is already used', 'Unique Node (uf_node) node with duplicate content could not be created');
// Check for false negative: Attempt to create a node with a unique text
$ref_nid = 2;
$edit = array();
$edit['title'] = $this->randomName(24);
$edit['body[und][0][value]'] = $this->randomName(48);
$edit['field_uf_node_node[und]'] = $ref_nid;
$this->drupalPost('node/add/uf-node', $edit, t('Save'));
$this->assertText($edit['body[und][0][value]'], 'Unique Node (uf_node) node has been created');
}
/**
* Test the unique requirement on a node reference field in the all scope.
*/
public function testUniqueAllNode() {
// Create a content type with a node reference field that is set to be
// unique
$edit = array();
$edit['name'] = 'Unique Node';
$edit['type'] = 'uf_node';
$this->drupalPost('admin/structure/types/add', $edit, t('Save content type'));
$this->assertText('The content type Unique Node has been added.', 'Content type added.');
$edit = array();
$edit['fields[_add_new_field][label]'] = 'Unique Node Node';
$edit['fields[_add_new_field][field_name]'] = 'uf_node_node';
$edit['fields[_add_new_field][type]'] = 'node_reference';
$edit['fields[_add_new_field][widget_type]'] = 'options_select';
$this->drupalPost('admin/structure/types/manage/uf_node/fields', $edit, t('Save'));
$this->assertText('These settings apply to the Unique Node Node field everywhere it is used.', 'Field added to content type.');
$edit = array();
$edit['field[settings][referenceable_types][uf_node]'] = 'uf_node';
$this->drupalPost('admin/structure/types/manage/uf_node/fields/field_uf_node_node/field-settings', $edit, t('Save field settings'));
$this->assertText('Updated field Unique Node Node field settings.', 'Node reference field configured.');
$edit = array();
$edit['unique_field_fields[field_uf_node_node]'] = 'field_uf_node_node';
$edit['unique_field_scope'] = UNIQUE_FIELD_SCOPE_ALL;
$this->drupalPost('admin/structure/types/manage/uf_node', $edit, t('Save content type'));
$this->assertText('The content type Unique Node has been updated.', 'Content type updated.');
// Create another content type with the same text field and set it to unique
$edit = array();
$edit['name'] = 'Unique Node 2';
$edit['type'] = 'uf_node2';
$this->drupalPost('admin/structure/types/add', $edit, t('Save content type'));
$this->assertText('The content type Unique Node 2 has been added.', 'Content type added.');
$edit = array();
$edit['fields[_add_existing_field][label]'] = 'Unique Node 2 Node';
$edit['fields[_add_existing_field][field_name]'] = 'field_uf_node_node';
$edit['fields[_add_existing_field][widget_type]'] = 'options_select';
$this->drupalPost('admin/structure/types/manage/uf_node2/fields', $edit, t('Save'));
$this->assertText('These settings apply only to the Unique Node 2 Node field when used in the Unique Node 2 type.', 'Field added to content type.');
$edit = array();
$edit['field[settings][referenceable_types][uf_node]'] = 'uf_node';
$this->drupalPost('admin/structure/types/manage/uf_node2/fields/field_uf_node_node/field-settings', $edit, t('Save field settings'));
$this->assertText('Updated field Unique Node 2 Node field settings.', 'Node reference field configured.');
$edit = array();
$edit['unique_field_fields[field_uf_node_node]'] = 'field_uf_node_node';
$edit['unique_field_scope'] = UNIQUE_FIELD_SCOPE_ALL;
$this->drupalPost('admin/structure/types/manage/uf_node2', $edit, t('Save content type'));
$this->assertText('The content type Unique Node 2 has been updated.', 'Content type updated.');
// Create a node to be referenced
$edit = array();
$edit['title'] = $this->randomName(24);
$edit['body[und][0][value]'] = $this->randomName(48);
$this->drupalPost('node/add/uf-node', $edit, t('Save'));
$this->assertText($edit['body[und][0][value]'], 'Unique Node (uf_node) node has been created');
// Attempt to create 2 nodes that reference the same node in different
// content types
$ref_nid = 1;
$edit = array();
$edit['title'] = $this->randomName(24);
$edit['body[und][0][value]'] = $this->randomName(48);
$edit['field_uf_node_node[und]'] = $ref_nid;
$this->drupalPost('node/add/uf-node', $edit, t('Save'));
$this->assertText($edit['body[und][0][value]'], 'Unique Node (uf_node) node has been created');
$edit['body[und][0][value]'] = $this->randomName(48);
$this->drupalPost('node/add/uf-node2', $edit, t('Save'));
$this->assertText('The Unique Node 2 Node field requires a unique value, and the specified value is already used', 'Unique Node 2 (uf_node2) node with duplicate content could not be created');
// Check for false negative: Attempt to create a node with a unique text
$ref_nid = 2;
$edit = array();
$edit['title'] = $this->randomName(24);
$edit['body[und][0][value]'] = $this->randomName(48);
$edit['field_uf_node_node[und]'] = $ref_nid;
$this->drupalPost('node/add/uf-node2', $edit, t('Save'));
$this->assertText($edit['body[und][0][value]'], 'Unique Node 2 (uf_node2) node has been created');
}
/**
* Test the unique requirement on a node reference field in the single scope.
*/
public function testUniqueSingleNode() {
// Create a content type with two node reference fields that are set to be
// unique
$edit = array();
$edit['name'] = 'Unique Node Single';
$edit['type'] = 'uf_node_single';
$this->drupalPost('admin/structure/types/add', $edit, t('Save content type'));
$this->assertText('The content type Unique Node Single has been added.', 'Content type added.');
$edit = array();
$edit['fields[_add_new_field][label]'] = 'Unique Node Single Node 1';
$edit['fields[_add_new_field][field_name]'] = 'uf_node_single_node_1';
$edit['fields[_add_new_field][type]'] = 'node_reference';
$edit['fields[_add_new_field][widget_type]'] = 'options_select';
$this->drupalPost('admin/structure/types/manage/uf_node_single/fields', $edit, t('Save'));
$this->assertText('These settings apply to the Unique Node Single Node 1 field everywhere it is used.', 'Field added to content type.');
$edit = array();
$edit['field[settings][referenceable_types][uf_node_single]'] = 'uf_node_single';
$this->drupalPost('admin/structure/types/manage/uf_node_single/fields/field_uf_node_single_node_1/field-settings', $edit, t('Save field settings'));
$this->assertText('Updated field Unique Node Single Node 1 field settings.', 'Node reference field configured.');
$edit = array();
$edit['fields[_add_new_field][label]'] = 'Unique Node Single Node 2';
$edit['fields[_add_new_field][field_name]'] = 'uf_node_single_node_2';
$edit['fields[_add_new_field][type]'] = 'node_reference';
$edit['fields[_add_new_field][widget_type]'] = 'options_select';
$this->drupalPost('admin/structure/types/manage/uf_node_single/fields', $edit, t('Save'));
$this->assertText('These settings apply to the Unique Node Single Node 2 field everywhere it is used.', 'Field added to content type.');
$edit = array();
$edit['field[settings][referenceable_types][uf_node_single]'] = 'uf_node_single';
$this->drupalPost('admin/structure/types/manage/uf_node_single/fields/field_uf_node_single_node_2/field-settings', $edit, t('Save field settings'));
$this->assertText('Updated field Unique Node Single Node 2 field settings.', 'Node reference field configured.');
$edit = array();
$edit['unique_field_scope'] = UNIQUE_FIELD_SCOPE_NODE;
$edit['unique_field_fields[field_uf_node_single_node_1]'] = 'field_uf_node_single_node_1';
$edit['unique_field_fields[field_uf_node_single_node_2]'] = 'field_uf_node_single_node_2';
$this->drupalPost('admin/structure/types/manage/uf_node_single', $edit, t('Save content type'));
$this->assertText('The content type Unique Node Single has been updated.', 'Content type updated.');
// Create a node to be referenced
$edit = array();
$edit['title'] = $this->randomName(24);
$edit['body[und][0][value]'] = $this->randomName(48);
$this->drupalPost('node/add/uf-node-single', $edit, t('Save'));
$this->assertText($edit['body[und][0][value]'], 'Unique Node Single (uf_node_single) node has been created');
// Attempt to create a node with the same text in both fields
$ref_nid = 1;
$edit = array();
$edit['title'] = $this->randomName(24);
$edit['body[und][0][value]'] = $this->randomName(48);
$edit['field_uf_node_single_node_1[und]'] = $ref_nid;
$edit['field_uf_node_single_node_2[und]'] = $ref_nid;
$this->drupalPost('node/add/uf-node-single', $edit, t('Save'));
$this->assertText('The Unique Node Single Node 2 fields must have unique values. The Unique Node Single Node 2 field has a value that is already used.', 'Unique Node Single (uf_node_single) node with duplicate content could not be created');
// Check for false negative: Attempt to create a node with unique text
$edit = array();
$edit['title'] = $this->randomName(24);
$edit['body[und][0][value]'] = $this->randomName(48);
$this->drupalPost('node/add/uf-node-single', $edit, t('Save'));
$this->assertText($edit['body[und][0][value]'], 'Unique Node Single (uf_node_single) node has been created');
$edit = array();
$edit['title'] = $this->randomName(24);
$edit['body[und][0][value]'] = $this->randomName(48);
$edit['field_uf_node_single_node_1[und]'] = 1;
$edit['field_uf_node_single_node_2[und]'] = 2;
$this->drupalPost('node/add/uf-node-single', $edit, t('Save'));
$this->assertText($edit['body[und][0][value]'], 'Unique Node Single (uf_node_single) node has been created');
}
}

View File

@@ -0,0 +1,15 @@
name = Unique Field
description = "Provides content validation requirement that a node's author, language, taxonomy terms, or other fields are unique."
core = 7.x
files[] = unique_field.module
files[] = unique_field.install
files[] = tests/core.test
files[] = tests/date.test
files[] = tests/references.test
; Information added by drupal.org packaging script on 2011-07-26
version = "7.x-1.0-rc1"
core = "7.x"
project = "unique_field"
datestamp = "1311648120"

View File

@@ -0,0 +1,16 @@
<?php
/**
* @file
* Installation/uninstallation routines for the unique_field module.
*/
/**
* Implements hook_uninstall().
*
* Deletes all module variables and then clears the variables cache.
*/
function unique_field_uninstall() {
db_query("DELETE FROM {variable} WHERE name LIKE 'unique\_field\_%'");
cache_clear_all('variables', 'cache');
}

View File

@@ -0,0 +1,393 @@
<?php
/**
* @file
* Provides content validation requirement that a node's author,
* language, taxonomy terms, or other fields are unique.
*
* Unique Field module
* Compatible with Drupal 7.x
*
* By Joe Turgeon [http://arithmetric.com]
*/
// query scope definitions
define('UNIQUE_FIELD_SCOPE_NODE', 'node');
define('UNIQUE_FIELD_SCOPE_TYPE', 'type');
define('UNIQUE_FIELD_SCOPE_LANGUAGE', 'language');
define('UNIQUE_FIELD_SCOPE_ALL', 'all');
// query comparison definitions
define('UNIQUE_FIELD_COMP_EACH', 'each');
define('UNIQUE_FIELD_COMP_ALL', 'all');
// query field definitions
define('UNIQUE_FIELD_FIELDS_TITLE', 'title');
define('UNIQUE_FIELD_FIELDS_AUTHOR', 'name');
define('UNIQUE_FIELD_FIELDS_LANGUAGE', 'language');
// setting definition
define('UNIQUE_FIELD_SHOW_MATCHES', 'show_matches');
/**
* Implements hook_help().
*/
function unique_field_help($path, $arg) {
switch ($path) {
case 'admin/help#unique_field':
$output = '<p>' . t("The Unique Field module allows administrators to require that content supplied for specified fields is unique. For example, you may require that each node has a unique title or a different author. For configuration options, please see the <em>Unique Field restrictions</em> section of a content type's administration page.") . '</p>';
return $output;
}
}
/**
* Implements hook_permission().
*/
function unique_field_permission() {
return array(
'unique_field_perm_admin' => array(
'title' => t('Designate fields as unique'),
'description' => t('Configure whether any fields of a content type should be unique.'),
),
'unique_field_perm_bypass' => array(
'title' => t('Bypass requirement that fields are unique'),
'description' => t('Allows users to ignore errors about duplicate content and submit it anyway.'),
),
);
}
/**
* Implements hook_form_alter().
*/
function unique_field_form_alter(&$form, &$form_state, $form_id) {
if ($form_id === 'node_type_form' && user_access('unique_field_perm_admin') && isset($form['#node_type'])) {
unique_field_node_settings_form($form);
}
elseif (strpos($form_id, 'node_form') !== FALSE && user_access('unique_field_perm_bypass')) {
$form['unique_field_override'] = array(
'#type' => 'hidden',
'#default_value' => '0',
);
}
}
/**
* Implements hook_node_validate().
*/
function unique_field_node_validate($node, $form) {
// skip validation if deleting a node or if override value is set
if ((!empty($node->op) && !empty($node->delete) && $node->op === $node->delete) || (is_array($form) && isset($form['unique_field_override']) && is_array($form['unique_field_override']) && $form['unique_field_override']['#value'] && user_access('unique_field_perm_bypass'))) {
return;
}
// get list of unique fields for node type
$fields = variable_get('unique_field_fields_' . $node->type, array());
// check if there are unique fields for this node type
if (count($fields)) {
// get unique field settings for this node type
$scope = variable_get('unique_field_scope_' . $node->type, UNIQUE_FIELD_SCOPE_TYPE);
$comp = variable_get('unique_field_comp_' . $node->type, UNIQUE_FIELD_COMP_EACH);
$var_show_matches = variable_get('unique_field_show_matches_' . $node->type, array());
$show_matches = is_array($var_show_matches) && in_array(UNIQUE_FIELD_SHOW_MATCHES, $var_show_matches);
// initialization
$errmsg = NULL;
$errfld = array();
$matches = array();
$allmatch = NULL;
// check fields for node scope
if ($scope === UNIQUE_FIELD_SCOPE_NODE) {
$values = array();
foreach ($fields as $field) {
$new_values = array();
if ($field === UNIQUE_FIELD_FIELDS_TITLE) {
$new_values[] = $node->title;
}
else {
// Ensure that the field content exists. For example, if field
// permissions are enabled, the field may be missing for some users.
// Make sure to use the field API function, e.g. to respect language.
$fvalues = field_get_items('node', $node, $field);
if (empty($fvalues)) {
continue;
}
$field_info = field_info_field($field);
$field_keys = array_keys($field_info['columns']);
foreach ($fvalues as $index => $value) {
if (is_numeric($index)) {
$field_combined = array();
foreach ($field_keys as $key) {
if (isset($fvalues[$index][$key]) && !empty($fvalues[$index][$key])) {
$field_combined[$key] = $fvalues[$index][$key];
}
}
if (!empty($field_combined)) {
$new_values[] = serialize($field_combined);
}
}
}
}
$new_values = array_merge($values, $new_values);
$values = array_unique($new_values);
if (serialize($values) !== serialize($new_values)) {
$errfld[] = $field;
}
}
if (count($errfld) > 0) {
$errmsg = t('The @labels fields must have unique values. The @label field has a value that is already used.');
}
}
// check fields for other scopes
else {
foreach ($fields as $field) {
$values = '';
if ($field === UNIQUE_FIELD_FIELDS_TITLE) {
$values = $node->title;
}
elseif ($field === UNIQUE_FIELD_FIELDS_AUTHOR) {
$values = $node->uid;
}
elseif ($field === UNIQUE_FIELD_FIELDS_LANGUAGE) {
$values = $node->language;
}
else {
// Ensure that the field content exists. For example, if field
// permissions are enabled, the field may be missing for some users.
// Make sure to use the field API function, e.g. to respect language.
$fvalues = field_get_items('node', $node, $field);
if (empty($fvalues)) {
continue;
}
foreach ($fvalues as $index => $value) {
if (!is_numeric($index) || !is_array($value)) {
unset($fvalues[$index]);
}
}
$field_info = field_info_field($field);
$values = _field_filter_items($field_info, $fvalues);
}
if (empty($values)) {
continue;
}
$match = unique_field_match_value($field, $values, $scope, $node->type, $node->language);
// remove matches of this node
if ($node->nid && is_array($match) && in_array($node->nid, $match)) {
$key = array_search($node->nid, $match);
unset($match[$key]);
}
if ($comp === UNIQUE_FIELD_COMP_EACH && is_array($match) && count($match)) {
$errfld[] = $field;
$errmsg = t('The @label field requires a unique value, and the specified value is already used.');
}
$matches[$field] = $match;
$allmatch = is_array($allmatch) ? array_intersect($allmatch, $match) : $match;
}
// check for fields in combination
if ($comp === UNIQUE_FIELD_COMP_ALL && is_array($allmatch) && count($allmatch)) {
foreach ($fields as $field) {
$errfld[] = $field;
$matches[$field] = $allmatch;
}
$errmsg = t('This form requires that the fields @labels are a unique combination. The specified values are already used.');
}
}
// common error messages
if ($errmsg && !empty($errmsg) && is_array($errfld) && count($errfld) > 0) {
$labels = array();
foreach ($errfld as $field) {
if ($field === UNIQUE_FIELD_FIELDS_TITLE) {
$nodetype = node_type_get_type($node->type);
$labels[$field] = $nodetype->title_label;
}
elseif ($field === UNIQUE_FIELD_FIELDS_AUTHOR) {
$labels[$field] = t('Author');
}
elseif ($field === UNIQUE_FIELD_FIELDS_LANGUAGE) {
$labels[$field] = t('Language');
}
else {
$fld = field_info_instance('node', $field, $node->type);
$labels[$field] = $fld['label'];
}
}
foreach ($errfld as $field) {
$msg = strtr($errmsg, array('@label' => check_plain($labels[$field]), '@labels' => check_plain(join(', ', $labels))));
if ($show_matches && isset($matches[$field]) && is_array($matches[$field]) && count($matches[$field])) {
$list_items = array();
foreach ($matches[$field] as $nid) {
$match_node = node_load($nid);
if (node_access('view', $match_node)) {
$list_items[] = l($match_node->title, 'node/' . $nid);
}
}
$list_html = theme('item_list', array('items' => $list_items));
$msg .= ' ' . t('Matches are found in the following content: !list-html', array('!list-html' => $list_html));
}
if (user_access('unique_field_perm_bypass')) {
$form_id = str_replace('_', '-', $form['#id']);
$msg .= '<p>' . t('Click !here to bypass this check and resubmit.', array('!here' => "<a href=\"#\" onclick=\"jQuery('form#" . $form_id . " input[name=\'unique_field_override\']').val(1);jQuery('form#" . $form_id . "').submit();return false;\">" . t('here') . '</a>')) . '</p>';
}
form_set_error($field, $msg);
// if checking the fields in combination, then one error message
// is enough for all of the fields
if ($comp === UNIQUE_FIELD_COMP_ALL) {
break;
}
}
}
}
}
/**
* Find nodes with a matching field value within a given scope.
*/
function unique_field_match_value($field, $values, $scope, $ntype = NULL, $nlanguage = NULL) {
// Initialize EntityFieldQuery
$entity_type = 'node';
$efq = new EntityFieldQuery();
$efq->entityCondition('entity_type', $entity_type);
// Add query conditions depending on the type of field
if ($field === UNIQUE_FIELD_FIELDS_TITLE) {
// Query condition for bundle title property
$efq->propertyCondition('title', $values);
}
elseif ($field === UNIQUE_FIELD_FIELDS_AUTHOR) {
// Query condition for bundle author property
$efq->propertyCondition('uid', $values);
}
elseif ($field === UNIQUE_FIELD_FIELDS_LANGUAGE) {
// Query condition for bundle language property
$efq->propertyCondition('language', $values);
}
else {
// Query condition for other fields
$field_info = field_info_field($field);
// Check all items/values
foreach ($values as $index => $value) {
// Check all columns
foreach ($value as $key => $val) {
// Skip values that are not stored in the database
if (!isset($field_info['columns'][$key]) || !is_array($field_info['columns'][$key])) {
continue;
}
// Skip if the value is an empty string or is not a scalar
if (!strlen($val) || !is_scalar($val)) {
continue;
}
$efq->fieldCondition($field, $key, $val);
}
}
}
// Add query conditions for the scope setting
if ($scope === UNIQUE_FIELD_SCOPE_TYPE && !empty($ntype)) {
// Query condition for the bundle (content type) scope
$efq->entityCondition('bundle', $ntype);
}
elseif ($scope === UNIQUE_FIELD_SCOPE_LANGUAGE && !empty($nlanguage)) {
// Query condition for the language scope
$efq->propertyCondition('language', $nlanguage);
}
// Execute query and collect results
$result = $efq->execute();
$nids = array();
if (isset($result[$entity_type]) && is_array($result[$entity_type])) {
foreach ($result[$entity_type] as $item) {
if (!empty($item->nid)) {
$nids[] = $item->nid;
}
}
}
return array_unique($nids);
}
/**
* Add the unique field settings form to content type forms (node_type_form).
*/
function unique_field_node_settings_form(&$form) {
// load fields for content type
$ntype = $form['#node_type']->type;
$nodetype = !empty($ntype) ? node_type_get_type($ntype) : FALSE;
$fieldopts = array();
$fieldopts[UNIQUE_FIELD_FIELDS_TITLE] = ($nodetype && !empty($nodetype->title_label)) ? check_plain($nodetype->title_label) . ' (' . t('title') . ')' : t('Title');
$fieldopts[UNIQUE_FIELD_FIELDS_AUTHOR] = t('Author');
if (module_exists('locale') && !empty($ntype) && variable_get('language_content_type_' . $ntype, 0)) {
$fieldopts[UNIQUE_FIELD_FIELDS_LANGUAGE] = t('Language');
}
if (!empty($ntype)) {
$fields = field_info_instances('node', $ntype);
foreach ($fields as $fieldname => $info) {
$fieldopts[$fieldname] = $info['label'] . ' (' . $fieldname . ')';
}
}
// build the form
$form['unique_field'] = array(
'#type' => 'fieldset',
'#title' => t('Unique Field restrictions'),
'#weight' => 1,
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#group' => 'additional_settings',
);
$form['unique_field']['unique_field_fields'] = array(
'#type' => 'checkboxes',
'#title' => t('Choose the fields that should be unique'),
'#options' => $fieldopts,
'#default_value' => !empty($ntype) ? variable_get('unique_field_fields_' . $ntype, array()) : array(),
'#description' => t('After designating that certain fields should be unique, users will not be able to submit the content form to create a new node or update an existing one if it contains values in the designated fields that duplicate others.'),
);
$form['unique_field']['unique_field_scope'] = array(
'#type' => 'radios',
'#title' => t('Choose the scope for the unique values'),
'#options' => array(
UNIQUE_FIELD_SCOPE_TYPE => t('Content type'),
UNIQUE_FIELD_SCOPE_LANGUAGE => t('Language'),
UNIQUE_FIELD_SCOPE_ALL => t('All nodes'),
UNIQUE_FIELD_SCOPE_NODE => t('Single node only'),
),
'#default_value' => !empty($ntype) ? variable_get('unique_field_scope_' . $ntype, UNIQUE_FIELD_SCOPE_TYPE) : UNIQUE_FIELD_SCOPE_TYPE,
'#description' => t('Choose whether the values in the specified fields must be unique among nodes of this content type, among nodes of the same language, among all nodes, or only among the fields of the present node.'),
);
$form['unique_field']['unique_field_comp'] = array(
'#type' => 'radios',
'#title' => t('Choose whether values must be unique individually or in combination'),
'#options' => array(
UNIQUE_FIELD_COMP_EACH => t('Each of the specified fields must have a unique value'),
UNIQUE_FIELD_COMP_ALL => t('The combination of values from the specified fields must be unique'),
),
'#default_value' => !empty($ntype) ? variable_get('unique_field_comp_' . $ntype, UNIQUE_FIELD_COMP_EACH) : UNIQUE_FIELD_COMP_EACH,
'#description' => t('For example, if you have fields for the parts of a street address (street number and name, city, and zip code) on a node, and want to allow only one node per complete address, but not only one node per city or per zip code, then you would want to choose that the fields must be unique in combination.'),
);
$form['unique_field']['unique_field_show_matches'] = array(
'#type' => 'checkboxes',
'#title' => t('Check this box to show which nodes match when duplicate values are found'),
'#options' => array(UNIQUE_FIELD_SHOW_MATCHES => t('Enabled')),
'#default_value' => !empty($ntype) ? variable_get('unique_field_show_matches_' . $ntype, array()) : array(),
);
// add validation function
$form['#validate'][] = 'unique_field_node_settings_form_validate';
}
/**
* Form validation callback for unique_field_node_settings_form.
*/
function unique_field_node_settings_form_validate($form, &$form_state) {
if ($form_state['values']['unique_field_scope'] === UNIQUE_FIELD_SCOPE_NODE) {
if ($form_state['values']['unique_field_comp'] === UNIQUE_FIELD_COMP_ALL) {
form_set_error('unique_field_comp', t('The scope of a single node requires that each field must be unique.'));
}
if (($form_state['values']['unique_field_fields'][UNIQUE_FIELD_FIELDS_AUTHOR] === UNIQUE_FIELD_FIELDS_AUTHOR) || (isset($form_state['values']['unique_field_fields'][UNIQUE_FIELD_FIELDS_LANGUAGE]) && $form_state['values']['unique_field_fields'][UNIQUE_FIELD_FIELDS_LANGUAGE] === UNIQUE_FIELD_FIELDS_LANGUAGE)) {
form_set_error('unique_field_fields', t('The author and language fields are not supported within the scope of a single node.'));
}
}
}