Révision b08d2851
Ajouté par Assos Assos il y a environ 10 ans
drupal7/sites/all/modules/fivestar/fivestar.info | ||
---|---|---|
7 | 7 |
files[] = test/fivestar.base.test |
8 | 8 |
files[] = test/fivestar.field.test |
9 | 9 |
|
10 |
; Information added by Drupal.org packaging script on 2014-03-11
|
|
11 |
version = "7.x-2.0-rc3"
|
|
10 |
; Information added by Drupal.org packaging script on 2014-03-17
|
|
11 |
version = "7.x-2.1"
|
|
12 | 12 |
core = "7.x" |
13 | 13 |
project = "fivestar" |
14 |
datestamp = "1394545353"
|
|
14 |
datestamp = "1395087839"
|
|
15 | 15 |
|
drupal7/sites/all/modules/flag/README.txt | ||
---|---|---|
53 | 53 |
------------------- |
54 | 54 |
- Views |
55 | 55 |
- Session API |
56 |
- Token, which is required for Flag to provide tokens on flagged entities. |
|
56 | 57 |
|
57 | 58 |
Installation |
58 | 59 |
------------ |
drupal7/sites/all/modules/flag/flag.info | ||
---|---|---|
4 | 4 |
package = Flags |
5 | 5 |
configure = admin/structure/flags |
6 | 6 |
|
7 |
test_dependencies[] = token |
|
8 |
test_dependencies[] = rules |
|
9 |
|
|
7 | 10 |
; Files that contain classes. |
8 | 11 |
; Flag classes |
9 | 12 |
files[] = includes/flag/flag_flag.inc |
... | ... | |
26 | 29 |
files[] = includes/views/flag_plugin_argument_validate_flaggability.inc |
27 | 30 |
files[] = tests/flag.test |
28 | 31 |
|
29 |
; Information added by Drupal.org packaging script on 2014-01-26
|
|
30 |
version = "7.x-3.3"
|
|
32 |
; Information added by Drupal.org packaging script on 2014-03-18
|
|
33 |
version = "7.x-3.4"
|
|
31 | 34 |
core = "7.x" |
32 | 35 |
project = "flag" |
33 |
datestamp = "1390732707"
|
|
36 |
datestamp = "1395128956"
|
|
34 | 37 |
|
drupal7/sites/all/modules/flag/flag.module | ||
---|---|---|
598 | 598 |
), |
599 | 599 |
); |
600 | 600 |
|
601 |
// Reset static cache to ensure all flag permissions are available. |
|
602 |
drupal_static_reset('flag_get_flags'); |
|
601 | 603 |
$flags = flag_get_flags(); |
602 | 604 |
// Provide flag and unflag permissions for each flag. |
603 | 605 |
foreach ($flags as $flag_name => $flag) { |
... | ... | |
1180 | 1182 |
function flag_user_account_removal($account) { |
1181 | 1183 |
// Remove flags by this user. |
1182 | 1184 |
$query = db_select('flagging', 'fc'); |
1183 |
$query->leftJoin('flag_counts', 'c', 'fc.entity_id = c.entity_id AND fc.entity_type = c.entity_type'); |
|
1185 |
$query->leftJoin('flag_counts', 'c', 'fc.entity_id = c.entity_id AND fc.entity_type = c.entity_type AND fc.fid = c.fid');
|
|
1184 | 1186 |
$result = $query |
1185 | 1187 |
->fields('fc', array('fid', 'entity_id')) |
1186 | 1188 |
->fields('c', array('count')) |
... | ... | |
1692 | 1694 |
/** |
1693 | 1695 |
* Get the count of flags for a particular entity type. |
1694 | 1696 |
* |
1697 |
* When called during a flagging or unflagging (such as from a hook |
|
1698 |
* implementation or from Rules), the flagging or unflagging that is in the |
|
1699 |
* process of being performed: |
|
1700 |
* - will be included during a flagging operation |
|
1701 |
* - will STILL be included during an unflagging operation. That is, the count |
|
1702 |
* will not yet have been decreased. |
|
1703 |
* This is because this queries the {flagging} table, which only has its record |
|
1704 |
* deleted at the very end of the unflagging process. |
|
1705 |
* |
|
1695 | 1706 |
* @param $flag |
1696 | 1707 |
* The flag. |
1697 | 1708 |
* @param $entity_type |
... | ... | |
1723 | 1734 |
/** |
1724 | 1735 |
* Get the user's flag count. |
1725 | 1736 |
* |
1737 |
* When called during a flagging or unflagging (such as from a hook |
|
1738 |
* implementation or from Rules), the flagging or unflagging that is in the |
|
1739 |
* process of being performed: |
|
1740 |
* - will be included during a flagging operation |
|
1741 |
* - will STILL be included during an unflagging operation. That is, the count |
|
1742 |
* will not yet have been decreased. |
|
1743 |
* This is because this queries the {flagging} table, which only has its record |
|
1744 |
* deleted at the very end of the unflagging process. |
|
1745 |
* |
|
1726 | 1746 |
* @param $flag |
1727 | 1747 |
* The flag. |
1728 | 1748 |
* @param $user |
... | ... | |
1754 | 1774 |
/** |
1755 | 1775 |
* Get flag counts for all flags on a node. |
1756 | 1776 |
* |
1777 |
* When called during a flagging or unflagging (such as from a hook |
|
1778 |
* implementation or from Rules), the count this returns takes into account the |
|
1779 |
* the flagging or unflagging that is in the process of being performed. |
|
1780 |
* |
|
1757 | 1781 |
* @param $entity_type |
1758 | 1782 |
* The entity type (usually 'node'). |
1759 | 1783 |
* @param $entity_id |
... | ... | |
1786 | 1810 |
/** |
1787 | 1811 |
* Get the total count of items flagged within a flag. |
1788 | 1812 |
* |
1813 |
* When called during a flagging or unflagging (such as from a hook |
|
1814 |
* implementation or from Rules), the count this returns takes into account the |
|
1815 |
* the flagging or unflagging that is in the process of being performed. |
|
1816 |
* |
|
1789 | 1817 |
* @param $flag_name |
1790 | 1818 |
* The flag name for which to retrieve a flag count. |
1791 | 1819 |
* @param $reset |
... | ... | |
1800 | 1828 |
if (!isset($counts[$flag_name])) { |
1801 | 1829 |
$flag = flag_get_flag($flag_name); |
1802 | 1830 |
$counts[$flag_name] = db_select('flag_counts', 'fc') |
1803 |
->fields('fc', array('flagging_id'))
|
|
1831 |
->fields('fc', array('fid')) |
|
1804 | 1832 |
->condition('fid', $flag->fid) |
1805 | 1833 |
->countQuery() |
1806 | 1834 |
->execute() |
... | ... | |
2014 | 2042 |
/** |
2015 | 2043 |
* Find what a user has flagged, either a single entity or on the entire site. |
2016 | 2044 |
* |
2045 |
* When called during a flagging or unflagging (such as from a hook |
|
2046 |
* implementation or from Rules), the flagging or unflagging that is in the |
|
2047 |
* process of being performed: |
|
2048 |
* - will be included during a flagging operation |
|
2049 |
* - will STILL be included during an unflagging operation. That is, the count |
|
2050 |
* will not yet have been decreased. |
|
2051 |
* This is because this queries the {flagging} table, which only has its record |
|
2052 |
* deleted at the very end of the unflagging process. |
|
2053 |
* |
|
2017 | 2054 |
* @param $entity_type |
2018 | 2055 |
* The type of entity that will be retrieved. Usually 'node'. |
2019 | 2056 |
* @param $entity_id |
... | ... | |
2089 | 2126 |
/** |
2090 | 2127 |
* Return a list of users who have flagged an entity. |
2091 | 2128 |
* |
2129 |
* When called during a flagging or unflagging (such as from a hook |
|
2130 |
* implementation or from Rules), the flagging or unflagging that is in the |
|
2131 |
* process of being performed: |
|
2132 |
* - will be included during a flagging operation |
|
2133 |
* - will STILL be included during an unflagging operation. That is, the count |
|
2134 |
* will not yet have been decreased. |
|
2135 |
* This is because this queries the {flagging} table, which only has its record |
|
2136 |
* deleted at the very end of the unflagging process. |
|
2137 |
* |
|
2092 | 2138 |
* @param $entity_type |
2093 | 2139 |
* The type of entity that will be retrieved. Usually 'node'. |
2094 | 2140 |
* @param $entity_id |
... | ... | |
2097 | 2143 |
* (optional) The name of a flag if wanting a list specific to a single flag. |
2098 | 2144 |
* |
2099 | 2145 |
* @return |
2100 |
* If no flag name is given, an array of flagging data, keyed by the user |
|
2101 |
* ID that flagged the entity. Each flagging array is structured as |
|
2102 |
* an array of flag information for each flag, keyed by the flag name. If |
|
2103 |
* a flag name is specified, only the information for that flag is returned. |
|
2146 |
* A nested array of flagging records (i.e. rows from the {flagging} table, |
|
2147 |
* rather than complete Flagging entities). The structure depends on the |
|
2148 |
* presence of the $flag_name parameter: |
|
2149 |
* - if $flag_name is omitted, the array is keyed first by the user ID of |
|
2150 |
* the users that flagged the entity, then by flag name. Each value is |
|
2151 |
* then the flagging record. |
|
2152 |
* - if $flag_name is given, the array is keyed only by user ID. Each value |
|
2153 |
* is the flagging record. |
|
2104 | 2154 |
* If no flags were found an empty array is returned. |
2105 | 2155 |
*/ |
2106 | 2156 |
function flag_get_entity_flags($entity_type, $entity_id, $flag_name = NULL) { |
drupal7/sites/all/modules/flag/flag.rules.inc | ||
---|---|---|
221 | 221 |
'provides' => array( |
222 | 222 |
'overall_flag_count' => array( |
223 | 223 |
'label' => t('Overall flag count'), |
224 |
'description' => t('During a flagging/unflagging event the count |
|
225 |
will take into account the current flagging/unflagging procedure.'), |
|
224 | 226 |
'type' => 'integer', |
225 | 227 |
), |
226 | 228 |
), |
... | ... | |
245 | 247 |
'provides' => array( |
246 | 248 |
'entity_flag_count' => array( |
247 | 249 |
'label' => t('Entity flag count'), |
250 |
'description' => t('During a flagging event, the count |
|
251 |
will take into account the current flagging procedure. For |
|
252 |
an unflagging event, the count will NOT yet be decreased for the |
|
253 |
current unflagging procedure.'), |
|
248 | 254 |
'type' => 'integer', |
249 | 255 |
), |
250 | 256 |
), |
... | ... | |
267 | 273 |
'provides' => array( |
268 | 274 |
'user_flag_count' => array( |
269 | 275 |
'label' => t('User flag count'), |
276 |
'description' => t('During a flagging event, the count |
|
277 |
will take into account the current flagging procedure. For |
|
278 |
an unflagging event, the count will NOT yet be decreased for the |
|
279 |
current unflagging procedure.'), |
|
270 | 280 |
'type' => 'integer', |
271 | 281 |
), |
272 | 282 |
), |
... | ... | |
435 | 445 |
|
436 | 446 |
/** |
437 | 447 |
* Base action implementation: Fetch overall count for a particular flag. |
448 |
* |
|
449 |
* The count that is returned during a flagging or an unflagging will take into |
|
450 |
* account the current flag/unflag process. |
|
438 | 451 |
*/ |
439 | 452 |
function flag_rules_action_fetch_overall_flag_count($flag) { |
440 | 453 |
$count = flag_get_flag_counts($flag->name); |
... | ... | |
457 | 470 |
|
458 | 471 |
/** |
459 | 472 |
* Base action implementation: Fetch count of flags for a particular entity type. |
473 |
* |
|
474 |
* During a flagging, the current flagging will be included in the count. |
|
475 |
* During an unflagging, the current flagging being removed will not yet have |
|
476 |
* been removed from the count. |
|
460 | 477 |
*/ |
461 | 478 |
function flag_rules_action_fetch_entity_flag_count($flag, $entity_type) { |
462 | 479 |
$count = flag_get_entity_flag_counts($flag, $entity_type); |
... | ... | |
465 | 482 |
|
466 | 483 |
/** |
467 | 484 |
* Base action implementation: Fetch user's flag count. |
485 |
* |
|
486 |
* During a flagging, the current flagging will be included in the count. |
|
487 |
* During an unflagging, the current flagging will not yet have been removed |
|
488 |
* from the count. |
|
468 | 489 |
*/ |
469 | 490 |
function flag_rules_action_fetch_user_flag_count($flag, $user) { |
470 | 491 |
$count = flag_get_user_flag_counts($flag, $user); |
... | ... | |
497 | 518 |
'number' => array( |
498 | 519 |
'type' => 'integer', |
499 | 520 |
'label' => t('Number'), |
500 |
'description' => t('The number against which to test the number of times the object is flagged. For example, if you type "3" here, and choose "Greater than" for the operator, then this condition will return TRUE if the object is flagged more than three times.'), |
|
521 |
'description' => t('The number against which to test the number of |
|
522 |
times the object is flagged. For example, if you type "3" here, |
|
523 |
and choose "Greater than" for the operator, then this condition |
|
524 |
will return TRUE if the object is flagged more than three times. |
|
525 |
During a flagging or an unflagging event the count will take into |
|
526 |
account the current flag/unflag process.'), |
|
501 | 527 |
), |
502 | 528 |
'operator' => array( |
503 | 529 |
'type' => 'text', |
... | ... | |
554 | 580 |
|
555 | 581 |
/** |
556 | 582 |
* Condition: Check flagging count. |
583 |
* |
|
584 |
* The count that is returned during a flagging or an unflagging will take into |
|
585 |
* acount the current flag/unflag process. |
|
557 | 586 |
*/ |
558 | 587 |
function flag_rules_condition_threshold($flag, $entity, $number, $operator = '=') { |
559 | 588 |
$count = $flag->get_count($flag->get_entity_id($entity)); |
drupal7/sites/all/modules/flag/flag.tokens.inc | ||
---|---|---|
7 | 7 |
|
8 | 8 |
/** |
9 | 9 |
* Implements of hook_token_info(). |
10 |
* |
|
11 |
* The tokens we provide on generic entities require token module. |
|
10 | 12 |
*/ |
11 | 13 |
function flag_token_info() { |
12 | 14 |
$types = array(); |
... | ... | |
74 | 76 |
'description' => t('The current count total for this flag.'), |
75 | 77 |
); |
76 | 78 |
|
77 |
// Add tokens for the flag count available at the node/comment/user level. |
|
78 |
foreach (flag_get_types() as $flag_type) { |
|
79 |
$flags = flag_get_flags($flag_type); |
|
80 |
foreach ($flags as $flag) { |
|
81 |
$tokens[$flag_type]['flag-' . str_replace('_', '-', $flag->name) . '-count'] = array( |
|
82 |
'name' => t('@flag flag count', array('@flag' => $flag->get_title())), |
|
83 |
'description' => t('Total flag count for flag @flag', array('@flag' => $flag->get_title())), |
|
84 |
); |
|
85 |
$tokens[$flag_type]['flag-' . str_replace('_', '-', $flag->name) . '-link'] = array( |
|
86 |
'name' => t('@flag flag link', array('@flag' => $flag->get_title())), |
|
87 |
'description' => t('Flag/unflag link for @flag', array('@flag' => $flag->get_title())), |
|
88 |
); |
|
79 |
// Add tokens for the flag count available at the entity level. |
|
80 |
// These require token module because we need its helper data and functions |
|
81 |
// to deal with token types that are not the same as the entity types they are |
|
82 |
// for (in particular, terms and vocabularies). |
|
83 |
if (module_exists('token')) { |
|
84 |
$entity_info = entity_get_info(); |
|
85 |
foreach (flag_get_types() as $flag_type) { |
|
86 |
// The flag type is the entity type, but this is not necessarily the same |
|
87 |
// as the entity's token type. |
|
88 |
$token_type = $entity_info[$flag_type]['token type']; |
|
89 |
$flags = flag_get_flags($flag_type); |
|
90 |
foreach ($flags as $flag) { |
|
91 |
$tokens[$token_type]['flag-' . str_replace('_', '-', $flag->name) . '-count'] = array( |
|
92 |
'name' => t('@flag flag count', array('@flag' => $flag->get_title())), |
|
93 |
'description' => t('Total flag count for flag @flag', array('@flag' => $flag->get_title())), |
|
94 |
'flag-type' => $flag_type, |
|
95 |
); |
|
96 |
$tokens[$token_type]['flag-' . str_replace('_', '-', $flag->name) . '-link'] = array( |
|
97 |
'name' => t('@flag flag link', array('@flag' => $flag->get_title())), |
|
98 |
'description' => t('Flag/unflag link for @flag', array('@flag' => $flag->get_title())), |
|
99 |
'flag-type' => $flag_type, |
|
100 |
); |
|
101 |
} |
|
89 | 102 |
} |
90 | 103 |
} |
91 | 104 |
|
... | ... | |
155 | 168 |
} |
156 | 169 |
} |
157 | 170 |
|
158 |
if (isset($data[$type]) && in_array($type, flag_get_types())) { |
|
159 |
$flags = flag_get_flags($type); |
|
160 |
$object = $data[$type]; |
|
161 |
foreach ($flags as $flag) { |
|
162 |
foreach ($tokens as $name => $original) { |
|
163 |
$flag_count_token = 'flag-' . str_replace('_', '-', $flag->name) . '-count'; |
|
164 |
$flag_link_token = 'flag-' . str_replace('_', '-', $flag->name) . '-link'; |
|
165 |
if ($name == $flag_count_token) { |
|
166 |
$replacements[$original] = $flag->get_count($flag->get_entity_id($object)); |
|
167 |
} |
|
168 |
elseif ($name == $flag_link_token) { |
|
169 |
$replacements[$original] = flag_create_link($flag->name, $flag->get_entity_id($object)); |
|
171 |
// We only provide tokens on entity types if we have token module's helper |
|
172 |
// methods available. |
|
173 |
if (isset($data[$type]) && module_exists('token')) { |
|
174 |
$entity_type = token_get_entity_mapping('token', $type); |
|
175 |
if ($entity_type && in_array($entity_type, flag_get_types())) { |
|
176 |
$flags = flag_get_flags($entity_type); |
|
177 |
$object = $data[$type]; |
|
178 |
foreach ($flags as $flag) { |
|
179 |
foreach ($tokens as $name => $original) { |
|
180 |
$flag_count_token = 'flag-' . str_replace('_', '-', $flag->name) . '-count'; |
|
181 |
$flag_link_token = 'flag-' . str_replace('_', '-', $flag->name) . '-link'; |
|
182 |
if ($name == $flag_count_token) { |
|
183 |
$replacements[$original] = $flag->get_count($flag->get_entity_id($object)); |
|
184 |
} |
|
185 |
elseif ($name == $flag_link_token) { |
|
186 |
$replacements[$original] = flag_create_link($flag->name, $flag->get_entity_id($object)); |
|
187 |
} |
|
170 | 188 |
} |
171 | 189 |
} |
172 | 190 |
} |
drupal7/sites/all/modules/flag/flag_actions.info | ||
---|---|---|
7 | 7 |
|
8 | 8 |
files[] = flag.install |
9 | 9 |
files[] = flag_actions.module |
10 |
; Information added by Drupal.org packaging script on 2014-01-26
|
|
11 |
version = "7.x-3.3"
|
|
10 |
; Information added by Drupal.org packaging script on 2014-03-18
|
|
11 |
version = "7.x-3.4"
|
|
12 | 12 |
core = "7.x" |
13 | 13 |
project = "flag" |
14 |
datestamp = "1390732707"
|
|
14 |
datestamp = "1395128956"
|
|
15 | 15 |
|
drupal7/sites/all/modules/flag/flag_bookmark/flag_bookmark.info | ||
---|---|---|
4 | 4 |
dependencies[] = flag |
5 | 5 |
package = Flags |
6 | 6 |
|
7 |
; Information added by Drupal.org packaging script on 2014-01-26
|
|
8 |
version = "7.x-3.3"
|
|
7 |
; Information added by Drupal.org packaging script on 2014-03-18
|
|
8 |
version = "7.x-3.4"
|
|
9 | 9 |
core = "7.x" |
10 | 10 |
project = "flag" |
11 |
datestamp = "1390732707"
|
|
11 |
datestamp = "1395128956"
|
|
12 | 12 |
|
drupal7/sites/all/modules/flag/includes/flag/flag_entity.inc | ||
---|---|---|
51 | 51 |
// Add checkboxes to show flag link on each entity view mode. |
52 | 52 |
$options = array(); |
53 | 53 |
$defaults = array(); |
54 |
foreach ($entity_info['view modes'] as $name => $view_mode) { |
|
54 |
$entity_view_modes = $entity_info['view modes']; |
|
55 |
foreach ($entity_view_modes as $name => $view_mode) { |
|
55 | 56 |
$options[$name] = t('Display on @name view mode', array('@name' => $view_mode['label'])); |
56 | 57 |
$defaults[$name] = !empty($this->show_in_links[$name]) ? $name : 0; |
57 | 58 |
} |
59 |
// Select the first display option by default if this is a new flag. |
|
60 |
if (empty($this->fid)) { |
|
61 |
$first_view_mode = reset(array_keys($entity_view_modes)); |
|
62 |
$defaults[$first_view_mode] = $first_view_mode; |
|
63 |
} |
|
58 | 64 |
|
59 | 65 |
$form['display']['show_in_links'] = array( |
60 | 66 |
'#type' => 'checkboxes', |
drupal7/sites/all/modules/flag/includes/flag/flag_flag.inc | ||
---|---|---|
676 | 676 |
* Flag the item even if the $account user don't have permission to do so. |
677 | 677 |
* @param $flagging |
678 | 678 |
* (optional) This method works in tandem with Drupal's Field subsystem. |
679 |
* Pass in a flagging entity if you want operate on it as well. |
|
679 |
* Pass in a Flagging entity if you want operate on it as well. This may be |
|
680 |
* used either of the following cases: |
|
681 |
* - to save field data on a new Flagging entity at the same time as |
|
682 |
* flagging an entity. In this case, using Entity API's entity_create() |
|
683 |
* is recommended, although the Flagging entity may also be created |
|
684 |
* directly as a new stdClass object. |
|
685 |
* - to update field data an existing flagging. The $action parameter should |
|
686 |
* be set to 'flag'. The Flagging entity will need to be loaded first with |
|
687 |
* flagging_load(). |
|
680 | 688 |
* |
681 | 689 |
* @return |
682 | 690 |
* FALSE if some error occured (e.g., user has no permission, flag isn't |
... | ... | |
712 | 720 |
return FALSE; |
713 | 721 |
} |
714 | 722 |
|
715 |
// Clear various caches; We don't want code running after us to report |
|
716 |
// wrong counts or false flaggings. |
|
717 |
drupal_static_reset('flag_get_counts'); |
|
718 |
drupal_static_reset('flag_get_user_flags'); |
|
719 |
drupal_static_reset('flag_get_entity_flags'); |
|
720 |
|
|
721 | 723 |
// Find out which user id to use. |
722 | 724 |
$uid = $this->global ? 0 : $account->uid; |
723 | 725 |
|
... | ... | |
788 | 790 |
} |
789 | 791 |
} |
790 | 792 |
elseif ($action == 'flag') { |
791 |
// Invoke hook_entity_presave(). |
|
792 |
module_invoke_all('entity_presave', $flagging, 'flagging'); |
|
793 |
|
|
794 | 793 |
if ($this->uses_anonymous_cookies()) { |
795 | 794 |
$this->_flag_anonymous($entity_id); |
796 | 795 |
} |
796 |
|
|
797 |
// By definition there is no flagging entity yet, but we may have been |
|
798 |
// passed one in to save. |
|
799 |
if (!isset($flagging)) { |
|
800 |
// Construct a new flagging object if we weren't passed one. |
|
801 |
$flagging = $this->new_flagging($entity_id, $uid, $sid); |
|
802 |
} |
|
803 |
|
|
804 |
// Invoke hook_entity_presave(). |
|
805 |
// In the case that we have been passed in a flagging entity to update, |
|
806 |
// this is technically still a presave, even though the {flagging} table |
|
807 |
// itself is not changed. |
|
808 |
module_invoke_all('entity_presave', $flagging, 'flagging'); |
|
809 |
|
|
797 | 810 |
if (!$flagged) { |
798 |
// The entity is unflagged. By definition there is no flagging entity, |
|
799 |
// but we may have been passed one in to save. |
|
800 |
if (!isset($flagging)) { |
|
801 |
// Construct a new flagging object if we don't have one. |
|
802 |
$flagging = $this->new_flagging($entity_id, $uid, $sid); |
|
803 |
} |
|
811 |
// The entity is unflagged. |
|
812 |
|
|
804 | 813 |
// Save the flagging entity (just our table). |
805 |
$flagging_id = $this->_flag($entity_id, $uid, $sid); |
|
806 |
// The _flag() method is a plain DB record writer, so it's a bit |
|
807 |
// antiquated. We have to explicitly get our new ID out. |
|
808 |
$flagging->flagging_id = $flagging_id; |
|
814 |
$this->_flag($flagging); |
|
809 | 815 |
$this->_increase_count($entity_id); |
810 | 816 |
// We're writing out a flagging entity even when we aren't passed one |
811 | 817 |
// (e.g., when flagging via JavaScript toggle links); in this case |
... | ... | |
830 | 836 |
else { |
831 | 837 |
// Nothing to do. Item is already flagged. |
832 | 838 |
// |
833 |
// Except in the case a $flagging object is passed in: in this case
|
|
834 |
// we're, for example, arriving from an editing form and need to update
|
|
835 |
// the entity.
|
|
836 |
if ($flagging) {
|
|
839 |
// Except in the case a $flagging object is one that was passed in: in
|
|
840 |
// this case we're being requested to update the flagging. This occurs,
|
|
841 |
// for example, when the user updates the flagging entity form.
|
|
842 |
if (!empty($flagging->flagging_id)) {
|
|
837 | 843 |
$this->_update_flagging($flagging); |
838 | 844 |
} |
839 | 845 |
} |
... | ... | |
862 | 868 |
// Update the cache. |
863 | 869 |
entity_get_controller('flagging')->resetCache(); |
864 | 870 |
// Invoke hook_entity_update(). |
871 |
// Since there are no fields on the {flagging} table that can be |
|
872 |
// meaningfully changed, we don't perform an update on it. However, this |
|
873 |
// technically still counts as updating the flagging entity, since we update |
|
874 |
// its fields. |
|
865 | 875 |
module_invoke_all('entity_update', $flagging, 'flagging'); |
866 | 876 |
} |
867 | 877 |
private function _delete_flagging($flagging) { |
... | ... | |
892 | 902 |
return (object) array( |
893 | 903 |
'flagging_id' => NULL, |
894 | 904 |
'flag_name' => $this->name, |
905 |
'fid' => $this->fid, |
|
906 |
'entity_type' => $this->entity_type, |
|
895 | 907 |
'entity_id' => $entity_id, |
896 | 908 |
'uid' => $uid, |
897 | 909 |
'sid' => $sid, |
910 |
// The timestamp is not set until this is saved. |
|
898 | 911 |
); |
899 | 912 |
} |
900 | 913 |
|
... | ... | |
979 | 992 |
* You probably shouldn't call this raw private method: call the flag() |
980 | 993 |
* function instead. |
981 | 994 |
* |
982 |
* @return
|
|
983 |
* The 'flagging_id' column of the new {flagging} record.
|
|
995 |
* @param
|
|
996 |
* The flagging record.
|
|
984 | 997 |
* |
985 | 998 |
* @private |
986 | 999 |
*/ |
987 |
function _flag($entity_id, $uid, $sid) { |
|
988 |
$flagging_id = db_insert('flagging') |
|
989 |
->fields(array( |
|
990 |
'fid' => $this->fid, |
|
991 |
'entity_type' => $this->entity_type, |
|
992 |
'entity_id' => $entity_id, |
|
993 |
'uid' => $uid, |
|
994 |
'sid' => $sid, |
|
995 |
'timestamp' => REQUEST_TIME, |
|
996 |
)) |
|
997 |
->execute(); |
|
998 |
return $flagging_id; |
|
1000 |
function _flag($flagging) { |
|
1001 |
// Set the timestamp. |
|
1002 |
$flagging->timestamp = REQUEST_TIME; |
|
1003 |
|
|
1004 |
drupal_write_record('flagging', $flagging); |
|
1005 |
|
|
1006 |
// Clear various caches; we don't want code running after us to report |
|
1007 |
// wrong counts or false flaggings. |
|
1008 |
drupal_static_reset('flag_get_user_flags'); |
|
1009 |
drupal_static_reset('flag_get_entity_flags'); |
|
1010 |
// Despite being named in the same pattern as the count API functions, these |
|
1011 |
// query the {flagging} table, so are reset here. |
|
1012 |
drupal_static_reset('flag_get_entity_flag_counts'); |
|
1013 |
drupal_static_reset('flag_get_user_flag_counts'); |
|
999 | 1014 |
} |
1000 | 1015 |
|
1001 | 1016 |
/** |
... | ... | |
1008 | 1023 |
*/ |
1009 | 1024 |
function _unflag($entity_id, $flagging_id) { |
1010 | 1025 |
db_delete('flagging')->condition('flagging_id', $flagging_id)->execute(); |
1026 |
|
|
1027 |
// Clear various caches; we don't want code running after us to report |
|
1028 |
// wrong counts or false flaggings. |
|
1029 |
drupal_static_reset('flag_get_user_flags'); |
|
1030 |
drupal_static_reset('flag_get_entity_flags'); |
|
1031 |
// Despite being named in the same pattern as the count API functions, these |
|
1032 |
// query the {flagging} table, so are reset here. |
|
1033 |
drupal_static_reset('flag_get_entity_flag_counts'); |
|
1034 |
drupal_static_reset('flag_get_user_flag_counts'); |
|
1011 | 1035 |
} |
1012 | 1036 |
|
1013 | 1037 |
/** |
1014 |
* Increases the flag count for an object. |
|
1038 |
* Increases the flag count for an object and clears the static counts cache.
|
|
1015 | 1039 |
* |
1016 | 1040 |
* @param $entity_id |
1017 | 1041 |
* For which item should the count be increased. |
... | ... | |
1036 | 1060 |
)) |
1037 | 1061 |
->expression('count', 'count + :inc', array(':inc' => $number)) |
1038 | 1062 |
->execute(); |
1063 |
|
|
1064 |
// Reset the static cache of flag counts, so code running after this gets |
|
1065 |
// correct counts. |
|
1066 |
drupal_static_reset('flag_get_counts'); |
|
1067 |
drupal_static_reset('flag_get_flag_counts'); |
|
1039 | 1068 |
} |
1040 | 1069 |
|
1041 | 1070 |
/** |
1042 |
* Decreases the flag count for an object. |
|
1071 |
* Decreases the flag count for an object and clears the static counts cache.
|
|
1043 | 1072 |
* |
1044 | 1073 |
* @param $entity_id |
1045 | 1074 |
* For which item should the count be descreased. |
... | ... | |
1066 | 1095 |
->condition('fid', $this->fid) |
1067 | 1096 |
->condition('entity_id', $entity_id) |
1068 | 1097 |
->execute(); |
1098 |
|
|
1099 |
// Reset the static cache of flag counts, so code running after this gets |
|
1100 |
// correct counts. |
|
1101 |
drupal_static_reset('flag_get_counts'); |
|
1102 |
drupal_static_reset('flag_get_flag_counts'); |
|
1069 | 1103 |
} |
1070 | 1104 |
|
1071 | 1105 |
/** |
drupal7/sites/all/modules/flag/includes/flag/flag_node.inc | ||
---|---|---|
55 | 55 |
'#title' => t('Display checkbox on node edit form'), |
56 | 56 |
'#description' => t('If you elect to have a checkbox on the node edit form, you may specify its initial state in the settings form <a href="@content-types-url">for each content type</a>.', array('@content-types-url' => url('admin/structure/types'))), |
57 | 57 |
) + $form['display']['show_on_form']; |
58 |
|
|
59 |
// Add the 'teaser' view mode as a default value for the entity link display |
|
60 |
// option if this is a new flag. |
|
61 |
if (empty($this->fid)) { |
|
62 |
$form['display']['show_in_links']['#default_value']['teaser'] = 'teaser'; |
|
63 |
} |
|
58 | 64 |
} |
59 | 65 |
|
60 | 66 |
function type_access_multiple($entity_ids, $account) { |
drupal7/sites/all/modules/flag/plugins/access/flag_is_flagged/flag_is_flagged.inc | ||
---|---|---|
46 | 46 |
* Settings form. |
47 | 47 |
*/ |
48 | 48 |
function flag_flag_is_flagged_access_settings(&$form, &$form_state, $conf) { |
49 |
$options = array(); |
|
49 |
$flag_name_options = array();
|
|
50 | 50 |
|
51 | 51 |
$plugin = $form_state['plugin']; |
52 | 52 |
$entity_type = explode(':', $plugin['name']); |
53 | 53 |
$entity_type = $entity_type[1]; |
54 | 54 |
|
55 | 55 |
foreach (flag_get_flags($entity_type) as $flag) { |
56 |
$options[$flag->name] = check_plain($flag->title); |
|
56 |
$flag_name_options[$flag->name] = check_plain($flag->title);
|
|
57 | 57 |
} |
58 | 58 |
|
59 |
$flag_user_options = array( |
|
60 |
'any' => t('Flagged by anyone'), |
|
61 |
'user' => t('Flagged by current user'), |
|
62 |
); |
|
63 |
$flag_user_default = isset($conf['flag_user']) ? $conf['flag_user'] : 'user'; |
|
64 |
|
|
59 | 65 |
$form['settings']['flag_name'] = array( |
60 | 66 |
'#title' => t('Flag name'), |
61 | 67 |
'#type' => 'radios', |
62 |
'#options' => $options, |
|
68 |
'#options' => $flag_name_options,
|
|
63 | 69 |
'#description' => t('Include only flagged content.'), |
64 | 70 |
'#default_value' => $conf['flag_name'], |
65 | 71 |
); |
72 |
$form['settings']['flag_user'] = array( |
|
73 |
'#title' => t('Filter by flag owner'), |
|
74 |
'#type' => 'radios', |
|
75 |
'#options' => $flag_user_options, |
|
76 |
'#description' => t('Show content flagged by anyone or only by current user.'), |
|
77 |
'#default_value' => $flag_user_default, |
|
78 |
); |
|
66 | 79 |
return $form; |
67 | 80 |
} |
68 | 81 |
|
... | ... | |
78 | 91 |
// Get the ID of the entity. |
79 | 92 |
list($id) = entity_extract_ids($flag->entity_type, $context->data); |
80 | 93 |
|
81 |
return $flag->is_flagged($id); |
|
94 |
// Get either the count of users who have flagged this entity or find out |
|
95 |
// whether the current user has flagged this node, depending on settings. |
|
96 |
if (isset($conf['flag_user']) && $conf['flag_user'] == 'any') { |
|
97 |
$count = count(flag_get_entity_flags($flag->entity_type, $id, $conf['flag_name'])); |
|
98 |
return $count; |
|
99 |
} |
|
100 |
else { |
|
101 |
return $flag->is_flagged($id); |
|
102 |
} |
|
82 | 103 |
} |
83 | 104 |
|
84 | 105 |
/** |
... | ... | |
88 | 109 |
$flag = flag_get_flag($conf['flag_name']); |
89 | 110 |
|
90 | 111 |
if ($flag) { |
91 |
return t('@identifier is flagged with "@flag"', array('@flag' => check_plain($flag->title), '@identifier' => $context->identifier)); |
|
112 |
$flag_limit_by = ''; |
|
113 |
if (isset($conf['flag_user']) && $conf['flag_user'] == 'user') { |
|
114 |
return t('@identifier is flagged with "@flag" by current user.', array('@flag' => $flag->title, '@identifier' => $context->identifier)); |
|
115 |
} |
|
116 |
elseif (isset($conf['flag_user']) && $conf['flag_user'] == 'any') { |
|
117 |
return t('@identifier is flagged with "@flag" by anyone.', array('@flag' => $flag->title, '@identifier' => $context->identifier)); |
|
118 |
} |
|
92 | 119 |
} |
93 | 120 |
else { |
94 |
return t('Missing/ deleted flag "@flag"', array('@flag' => $conf['flag_name']));
|
|
121 |
return t('Missing/deleted flag "@flag"', array('@flag' => $conf['flag_name'])); |
|
95 | 122 |
} |
96 | 123 |
} |
drupal7/sites/all/modules/flag/plugins/content_types/flag_link/flag_link.inc | ||
---|---|---|
24 | 24 |
* Implements hook_PLUGIN_content_type_content_types(). |
25 | 25 |
*/ |
26 | 26 |
function flag_flag_link_content_type_content_types() { |
27 |
$types = &drupal_static(__FUNCTION__); |
|
28 |
if (isset($types)) { |
|
29 |
return $types; |
|
30 |
} |
|
31 |
|
|
27 | 32 |
$types = array(); |
28 | 33 |
$entities = entity_get_info(); |
29 | 34 |
|
30 | 35 |
foreach ($entities as $entity_type => $info) { |
31 |
if (entity_type_supports($entity_type, 'view')) { |
|
32 |
$types[$entity_type] = array( |
|
33 |
'title' => t('Flag for @entity_type', array('@entity_type' => $info['label'])), |
|
34 |
'category' => t('Entity'), |
|
35 |
'required context' => new ctools_context_required(t('Entity'), $entity_type), |
|
36 |
); |
|
37 |
} |
|
36 |
$types[$entity_type] = array( |
|
37 |
'title' => t('Flag for @entity_type', array('@entity_type' => $info['label'])), |
|
38 |
'category' => t('Entity'), |
|
39 |
'required context' => new ctools_context_required(t('Entity'), $entity_type), |
|
40 |
); |
|
38 | 41 |
} |
39 | 42 |
|
40 | 43 |
return $types; |
drupal7/sites/all/modules/flag/tests/flag.test | ||
---|---|---|
245 | 245 |
*/ |
246 | 246 |
function testFlagAdmin() { |
247 | 247 |
// Add a new flag using the UI. |
248 |
$this->drupalGet(FLAG_ADMIN_PATH . '/add/node'); |
|
249 |
|
|
250 |
// Check the form has the expected defaults. |
|
251 |
$this->assertFieldByName('flag_short', 'Flag this item', "The flag message default value shows in the form."); |
|
252 |
$this->assertFieldByName('unflag_short', 'Unflag this item', "The unflag message default value shows in the form."); |
|
253 |
$this->assertFieldByName('show_in_links[full]', 'full', "The view mode option is set to the node 'full' view mode by default."); |
|
254 |
$this->assertFieldByName('show_in_links[teaser]', 'teaser', "The view mode option is set to the node 'teaser' view mode by default."); |
|
255 |
|
|
248 | 256 |
$edit = array( |
249 | 257 |
'name' => drupal_strtolower($this->randomName()), |
250 | 258 |
'title' => $this->randomName(), |
... | ... | |
463 | 471 |
|
464 | 472 |
} |
465 | 473 |
|
474 |
/** |
|
475 |
* Tokens we provide on generic entities. |
|
476 |
*/ |
|
477 |
class FlagEntityTokensTestCase extends FlagTestCaseBase { |
|
478 |
|
|
479 |
/** |
|
480 |
* Implements getInfo(). |
|
481 |
*/ |
|
482 |
public static function getInfo() { |
|
483 |
return array( |
|
484 |
'name' => 'Flag: Entity tokens', |
|
485 |
'description' => 'Tokens for flag count on entities.', |
|
486 |
'group' => 'Flag', |
|
487 |
); |
|
488 |
} |
|
489 |
|
|
490 |
/** |
|
491 |
* Implements setUp(). |
|
492 |
*/ |
|
493 |
function setUp() { |
|
494 |
// Our entity tokens require token module. |
|
495 |
parent::setUp('flag', 'token'); |
|
496 |
} |
|
497 |
|
|
498 |
/** |
|
499 |
* Test tokens on nodes. |
|
500 |
*/ |
|
501 |
function testNodeFlagToken() { |
|
502 |
// Create a flag on article nodes. |
|
503 |
$flag_data = array( |
|
504 |
'entity_type' => 'node', |
|
505 |
'name' => 'node_flag', |
|
506 |
'title' => 'Node Flag', |
|
507 |
'global' => 0, |
|
508 |
'types' => array( |
|
509 |
0 => 'article', |
|
510 |
), |
|
511 |
'flag_short' => 'Flag this item', |
|
512 |
'flag_long' => '', |
|
513 |
'flag_message' => '', |
|
514 |
'unflag_short' => 'Unflag this item', |
|
515 |
'unflag_long' => '', |
|
516 |
'unflag_message' => '', |
|
517 |
'unflag_denied_text' => 'You may not unflag this item', |
|
518 |
'link_type' => 'normal', |
|
519 |
'weight' => 0, |
|
520 |
// Show the flag on the form. |
|
521 |
'show_on_form' => 1, |
|
522 |
'access_author' => '', |
|
523 |
'show_contextual_link' => 0, |
|
524 |
'show_in_links' => array( |
|
525 |
'full' => 1, |
|
526 |
'teaser' => 1, |
|
527 |
), |
|
528 |
'i18n' => 0, |
|
529 |
'api_version' => 3, |
|
530 |
); |
|
531 |
$flag = $this->createFlag($flag_data); |
|
532 |
|
|
533 |
// Create a node to flag. |
|
534 |
$node = (object) array( |
|
535 |
'type' => 'article', |
|
536 |
'title' => $this->randomName(), |
|
537 |
); |
|
538 |
node_save($node); |
|
539 |
|
|
540 |
// Flag it by several users. |
|
541 |
$flag_user_1 = $this->drupalCreateUser(array('flag node_flag',)); |
|
542 |
|
|
543 |
// Flag the node as the user. |
|
544 |
$flag = flag_get_flag('node_flag'); |
|
545 |
$flag->flag('flag', $node->nid, $flag_user_1); |
|
546 |
|
|
547 |
$flag_user_2 = $this->drupalCreateUser(array('flag node_flag',)); |
|
548 |
|
|
549 |
// Flag the node as the user. |
|
550 |
$flag->flag('flag', $node->nid, $flag_user_2); |
|
551 |
|
|
552 |
$text = '[node:flag-node-flag-count]'; |
|
553 |
|
|
554 |
$replaced_text = token_replace($text, array('node' => $node)); |
|
555 |
|
|
556 |
$this->assertEqual($replaced_text, 2, "The flag count token for the node is correct."); |
|
557 |
} |
|
558 |
|
|
559 |
/** |
|
560 |
* Test tokens on taxonomy terms. |
|
561 |
* |
|
562 |
* These are worthy of a separate test, as the token type is a special case. |
|
563 |
*/ |
|
564 |
function testTaxonomyTermFlagToken() { |
|
565 |
// Create a flag on tag terms. |
|
566 |
$flag_data = array( |
|
567 |
'entity_type' => 'taxonomy_term', |
|
568 |
'name' => 'term_flag', |
|
569 |
'title' => 'Term Flag', |
|
570 |
'global' => 0, |
|
571 |
'types' => array( |
|
572 |
0 => 'tags', |
|
573 |
), |
|
574 |
'flag_short' => 'Flag this item', |
|
575 |
'flag_long' => '', |
|
576 |
'flag_message' => '', |
|
577 |
'unflag_short' => 'Unflag this item', |
|
578 |
'unflag_long' => '', |
|
579 |
'unflag_message' => '', |
|
580 |
'unflag_denied_text' => 'You may not unflag this item', |
|
581 |
'link_type' => 'normal', |
|
582 |
'weight' => 0, |
|
583 |
// Show the flag on the form. |
|
584 |
'show_on_form' => 1, |
|
585 |
'access_author' => '', |
|
586 |
'show_contextual_link' => 0, |
|
587 |
'show_in_links' => array( |
|
588 |
'full' => 1, |
|
589 |
'teaser' => 1, |
|
590 |
), |
|
591 |
'i18n' => 0, |
|
592 |
'api_version' => 3, |
|
593 |
); |
|
594 |
$flag = $this->createFlag($flag_data); |
|
595 |
|
|
596 |
$vocabulary = taxonomy_vocabulary_load(1); |
|
597 |
|
|
598 |
// Create a term to flag. |
|
599 |
$term = (object) array( |
|
600 |
'name' => $this->randomName(), |
|
601 |
'vid' => 1, |
|
602 |
); |
|
603 |
taxonomy_term_save($term); |
|
604 |
|
|
605 |
// Flag it by several users. |
|
606 |
$flag_user_1 = $this->drupalCreateUser(array('flag term_flag',)); |
|
607 |
|
|
608 |
// Flag the term as the user. |
|
609 |
$flag = flag_get_flag('term_flag'); |
|
610 |
$flag->flag('flag', $term->tid, $flag_user_1); |
|
611 |
|
|
612 |
$flag_user_2 = $this->drupalCreateUser(array('flag term_flag',)); |
|
613 |
|
|
614 |
// Flag the term as the user. |
|
615 |
$flag = flag_get_flag('term_flag'); |
|
616 |
$flag->flag('flag', $term->tid, $flag_user_2); |
|
617 |
|
|
618 |
$text = '[term:flag-term-flag-count]'; |
|
619 |
|
|
620 |
$replaced_text = token_replace($text, array('term' => $term)); |
|
621 |
|
|
622 |
debug($replaced_text); |
|
623 |
|
|
624 |
$this->assertEqual($replaced_text, 2, "The flag count token for the term is correct."); |
|
625 |
} |
|
626 |
|
|
627 |
} |
|
628 |
|
|
466 | 629 |
/** |
467 | 630 |
* Access to flags using the basic flag link. |
468 | 631 |
*/ |
... | ... | |
642 | 805 |
'i18n' => 0, |
643 | 806 |
'api_version' => 3, |
644 | 807 |
); |
645 |
$flag = flag_flag::factory_by_array($this->flag_data); |
|
646 |
$flag->save(); |
|
808 |
$this->flag = flag_flag::factory_by_array($this->flag_data);
|
|
809 |
$this->flag->save();
|
|
647 | 810 |
// Reset our cache so our permissions show up. |
648 | 811 |
drupal_static_reset('flag_get_flags'); |
649 | 812 |
|
... | ... | |
651 | 814 |
$this->checkPermissions(array(), TRUE); |
652 | 815 |
|
653 | 816 |
// Create test user who can flag and unflag. |
654 |
$flag_unflag_user = $this->drupalCreateUser(array('flag test_flag', 'unflag test_flag')); |
|
655 |
$this->drupalLogin($flag_unflag_user); |
|
817 |
$this->flag_unflag_user = $this->drupalCreateUser(array('flag test_flag', 'unflag test_flag'));
|
|
818 |
$this->drupalLogin($this->flag_unflag_user);
|
|
656 | 819 |
|
657 | 820 |
// Create an article node to flag and unflag. |
658 | 821 |
$title = $this->randomName(8); |
... | ... | |
669 | 832 |
} |
670 | 833 |
|
671 | 834 |
/** |
672 |
* Test that a user sees the flag confirm form.
|
|
835 |
* Test usage of the flag confirm form.
|
|
673 | 836 |
*/ |
674 | 837 |
function testFlag() { |
675 | 838 |
// Look at our node. |
... | ... | |
694 | 857 |
$this->assertText($this->flag_data['flag_message'], 'The flag message appears once the item has been flagged.'); |
695 | 858 |
$this->assertLink($this->flag_data['unflag_short'], 0, 'The unflag link appears once the item has been flagged.'); |
696 | 859 |
|
860 |
// Reset the static cache before we get data from it. |
|
861 |
drupal_static_reset('flag_get_user_flags'); |
|
862 |
$this->assertTrue($this->flag->is_flagged($this->nid, $this->flag_unflag_user->uid), "The node is recorded as flagged by the user."); |
|
863 |
|
|
697 | 864 |
// Click the link to unflag the node. |
698 | 865 |
$this->clickLink($this->flag_data['unflag_short']); |
699 | 866 |
|
... | ... | |
709 | 876 |
$this->drupalPost(NULL, array(), $this->flag_data['unflag_short']); |
710 | 877 |
|
711 | 878 |
$this->assertText($this->flag_data['unflag_message'], 'The unflag message appears once the item has been flagged.'); |
879 |
|
|
880 |
// Reset the static cache before we get data from it. |
|
881 |
drupal_static_reset('flag_get_user_flags'); |
|
882 |
$this->assertFalse($this->flag->is_flagged($this->nid, $this->flag_unflag_user->uid), "The node is recorded as not flagged by the user."); |
|
712 | 883 |
} |
713 | 884 |
|
714 | 885 |
} |
... | ... | |
969 | 1140 |
} |
970 | 1141 |
|
971 | 1142 |
} |
1143 |
|
|
1144 |
/** |
|
1145 |
* Verifies the invocation of hooks when performing flagging and unflagging. |
|
1146 |
*/ |
|
1147 |
class FlagHookInvocationsTestCase extends FlagTestCaseBase { |
|
1148 |
|
|
1149 |
/** |
|
1150 |
* Implements getInfo(). |
|
1151 |
*/ |
|
1152 |
public static function getInfo() { |
|
1153 |
return array( |
|
1154 |
'name' => 'Flag hook invocations', |
|
1155 |
'description' => 'Invocation of flag and entity hooks and rules during flagging and unflagging.', |
|
1156 |
'group' => 'Flag', |
|
1157 |
); |
|
1158 |
} |
|
1159 |
|
|
1160 |
/** |
|
1161 |
* Implements setUp(). |
|
1162 |
*/ |
|
1163 |
function setUp() { |
|
1164 |
parent::setUp('flag', 'rules', 'flag_hook_test'); |
|
1165 |
|
|
1166 |
// Note the test module contains our test flag. |
|
1167 |
|
|
1168 |
// Create test user who can flag and unflag. |
|
1169 |
$this->flag_unflag_user = $this->drupalCreateUser(array('flag flag_hook_test_flag', 'unflag flag_hook_test_flag')); |
|
1170 |
$this->drupalLogin($this->flag_unflag_user); |
|
1171 |
} |
|
1172 |
|
|
1173 |
/** |
|
1174 |
* Test invocation of hooks and their data during flagging and unflagging. |
|
1175 |
* |
|
1176 |
* For each operation (flagging, re-flagging, unflagging) we test: |
|
1177 |
* - the order in which Flag hooks, entity hooks, and rules are invoked. |
|
1178 |
* - the parameters each hook receives |
|
1179 |
* - the data that a hook implementation obtains when it calls the Flag data |
|
1180 |
* API functions. |
|
1181 |
*/ |
|
1182 |
function testHookInvocation() { |
|
1183 |
// Create an article node that we try to create a flagging entity for. |
|
1184 |
$title = $this->randomName(8); |
|
1185 |
$node = array( |
|
1186 |
'title' => $title, |
|
1187 |
'body' => array(LANGUAGE_NONE => array(array('value' => $this->randomName(32)))), |
|
1188 |
'uid' => 1, |
|
1189 |
'type' => 'article', |
|
1190 |
'is_new' => TRUE, |
|
1191 |
); |
|
1192 |
$node = node_submit((object) $node); |
|
1193 |
node_save($node); |
|
1194 |
|
|
1195 |
// Initialize a tracking variable. The test module will update this when |
|
1196 |
// its hooks are invoked. |
|
1197 |
variable_set('flag_hook_test_hook_tracking', array()); |
|
1198 |
|
|
1199 |
// Flag the node as the user. |
|
1200 |
$flag = flag_get_flag('flag_hook_test_flag'); |
|
1201 |
$flag->flag('flag', $node->nid, $this->flag_unflag_user); |
|
1202 |
|
|
1203 |
// Get the variable the test module sets the hook order into. |
|
1204 |
$hook_data_variable = variable_get('flag_hook_test_hook_tracking', array()); |
|
1205 |
//debug($hook_data_variable['hook_entity_presave']); |
|
1206 |
//debug($hook_data_variable['hook_entity_insert']); |
|
1207 |
|
|
1208 |
$expected_hook_order = array( |
|
1209 |
'hook_entity_presave', |
|
1210 |
'hook_entity_insert', |
|
1211 |
'hook_flag_flag', |
|
1212 |
'rules_event', |
|
1213 |
); |
|
1214 |
|
|
1215 |
// Check the hooks are invoked in the correct order. |
|
1216 |
$this->assertIdentical($expected_hook_order, array_keys($hook_data_variable), "The hooks are invoked in the correct order when flagging a node."); |
|
1217 |
|
|
1218 |
// Check the parameters received by hook_entity_presave(). |
|
1219 |
// Param 0: $flagging. |
|
1220 |
// There is a bug with what is passed to entity hooks; for now, just check |
|
1221 |
// the things that do get passed. |
|
1222 |
// See https://drupal.org/node/2196055. |
|
1223 |
$this->assertEqual($hook_data_variable['hook_entity_presave']['parameters'][0]->flag_name, $flag->name, "The Flagging entity passed to hook_entity_presave() has the expected flag name property."); |
|
1224 |
//$this->assertEqual($hook_data_variable['hook_entity_insert']['parameters'][0]->fid, $flag->fid); |
|
1225 |
//$this->assertEqual($hook_data_variable['hook_entity_insert']['parameters'][0]->entity_type, 'node'); |
|
1226 |
$this->assertEqual($hook_data_variable['hook_entity_presave']['parameters'][0]->entity_id, $node->nid, "The Flagging entity passed to hook_entity_presave() has the expected entity ID property."); |
|
1227 |
$this->assertEqual($hook_data_variable['hook_entity_presave']['parameters'][0]->uid, $this->flag_unflag_user->uid, "The Flagging entity passed to hook_entity_presave() has the expected uid property."); |
|
1228 |
// Param 1: $entity_type. |
|
1229 |
$this->assertEqual($hook_data_variable['hook_entity_presave']['parameters'][1], 'flagging', "hook_entity_presave() is passed the correct entity type."); |
|
1230 |
|
|
1231 |
// Check the API data available to hook_entity_presave(). |
|
1232 |
//debug($hook_data_variable['hook_entity_presave']['api_calls']); |
|
1233 |
$this->assertEqual($hook_data_variable['hook_entity_presave']['api_calls']['flag_get_entity_flags'], array(), "hook_entity_presave() gets no data from from flag_get_entity_flags(), as the flagging has not yet taken place."); |
|
1234 |
$this->assertEqual($hook_data_variable['hook_entity_presave']['api_calls']['flag_get_user_flags'], array(), "hook_entity_presave() gets no data from from flag_get_user_flags(), as the flagging has not yet taken place."); |
|
1235 |
// The flag counts have not yet been increased. |
|
1236 |
$this->assertEqual($hook_data_variable['hook_entity_presave']['api_calls']['flag_get_counts'], array(), "hook_entity_presave() gets no data from from flag_get_counts(), as the flagging has not yet taken place."); |
|
1237 |
$this->assertEqual($hook_data_variable['hook_entity_presave']['api_calls']['flag_get_flag_counts'], 0, "hook_entity_presave() gets no data from from flag_get_flag_counts(), as the flagging has not yet taken place."); |
|
1238 |
$this->assertEqual($hook_data_variable['hook_entity_presave']['api_calls']['flag_get_entity_flag_counts'], 0, "hook_entity_presave() gets no data from from flag_get_entity_flag_counts(), as the flagging has not yet taken place."); |
|
1239 |
$this->assertEqual($hook_data_variable['hook_entity_presave']['api_calls']['flag_get_user_flag_counts'], 0, "hook_entity_presave() gets no data from from flag_get_user_flag_counts(), as the flagging has not yet taken place."); |
|
1240 |
|
|
1241 |
// Check the parameters received by hook_entity_insert(). |
|
1242 |
// Param 0: $flagging. |
|
1243 |
// See https://drupal.org/node/2196055. |
|
1244 |
$this->assertEqual($hook_data_variable['hook_entity_insert']['parameters'][0]->flag_name, $flag->name, "The Flagging entity passed to hook_entity_insert() has the expected flag name property."); |
|
1245 |
//$this->assertEqual($hook_data_variable['hook_entity_insert']['parameters'][0]->fid, $flag->fid); |
|
1246 |
//$this->assertEqual($hook_data_variable['hook_entity_insert']['parameters'][0]->entity_type, 'node'); |
|
1247 |
$this->assertEqual($hook_data_variable['hook_entity_insert']['parameters'][0]->entity_id, $node->nid, "The Flagging entity passed to hook_entity_insert() has the expected entity ID property."); |
|
1248 |
$this->assertEqual($hook_data_variable['hook_entity_insert']['parameters'][0]->uid, $this->flag_unflag_user->uid, "The Flagging entity passed to hook_entity_insert() has the expected uid property."); |
|
1249 |
$this->assertTrue(!empty($hook_data_variable['hook_entity_insert']['parameters'][0]->flagging_id), "The Flagging entity passed to hook_entity_insert() has the flagging ID set."); |
|
1250 |
// Param 1: $entity_type. |
|
1251 |
$this->assertEqual($hook_data_variable['hook_entity_insert']['parameters'][1], 'flagging', "hook_entity_insert() is passed the correct entity type."); |
|
1252 |
|
|
1253 |
// Check the API data available to hook_entity_insert(). |
|
1254 |
//debug($hook_data_variable['hook_entity_insert']['api_calls']); |
|
1255 |
$flag_get_entity_flags = $hook_data_variable['hook_entity_insert']['api_calls']['flag_get_entity_flags']; |
|
1256 |
$flag_get_entity_flags_uids = array_keys($flag_get_entity_flags); |
|
1257 |
$this->assertEqual($flag_get_entity_flags_uids, array($this->flag_unflag_user->uid), "hook_entity_insert() gets correct data for flagging users from flag_get_entity_flags()"); |
|
1258 |
$flag_get_entity_flags_flagging = $flag_get_entity_flags[$this->flag_unflag_user->uid]; |
|
1259 |
$this->assertEqual($flag_get_entity_flags_flagging->fid, $flag->fid, "hook_entity_insert() gets a correct fid on the Flagging obtained from flag_get_entity_flags()"); |
|
1260 |
$this->assertEqual($flag_get_entity_flags_flagging->entity_type, 'node', "hook_entity_insert() gets a correct entity type on the Flagging obtained from flag_get_entity_flags()"); |
|
1261 |
$this->assertEqual($flag_get_entity_flags_flagging->entity_id, $node->nid, "hook_entity_insert() gets a correct entity ID on the Flagging obtained from flag_get_entity_flags()"); |
|
1262 |
|
|
1263 |
$flag_get_user_flags = $hook_data_variable['hook_entity_insert']['api_calls']['flag_get_user_flags']; |
|
1264 |
$flag_get_user_flags_flagging = $flag_get_user_flags[$flag->name]; |
|
1265 |
$this->assertEqual($flag_get_user_flags_flagging->fid, $flag->fid, "hook_entity_insert() gets a correct fid on the Flagging obtained from flag_get_user_flags()"); |
|
1266 |
$this->assertEqual($flag_get_user_flags_flagging->entity_type, 'node', "hook_entity_insert() gets a correct entity type on the Flagging obtained from flag_get_user_flags()"); |
|
1267 |
$this->assertEqual($flag_get_user_flags_flagging->entity_id, $node->nid, "hook_entity_insert() gets a correct entity ID on the Flagging obtained from flag_get_user_flags()"); |
|
1268 |
|
|
1269 |
// The flag counts have been increased. |
|
1270 |
$flag_get_counts = $hook_data_variable['hook_entity_insert']['api_calls']['flag_get_counts']; |
|
1271 |
$this->assertEqual($flag_get_counts[$flag->name], 1, "hook_entity_insert() gets a correct flag count from flag_get_counts()."); |
|
1272 |
|
|
1273 |
$flag_get_flag_counts = $hook_data_variable['hook_entity_insert']['api_calls']['flag_get_flag_counts']; |
|
1274 |
$this->assertEqual($flag_get_flag_counts, 1, "hook_entity_insert() gets a correct flag count from flag_get_flag_counts()."); |
|
1275 |
|
|
1276 |
$flag_get_entity_flag_counts = $hook_data_variable['hook_entity_insert']['api_calls']['flag_get_entity_flag_counts']; |
|
1277 |
$this->assertEqual($flag_get_entity_flag_counts, 1, "hook_entity_insert() gets a correct flag count from flag_get_entity_flag_counts()."); |
|
1278 |
|
|
1279 |
$flag_get_user_flag_counts = $hook_data_variable['hook_entity_insert']['api_calls']['flag_get_user_flag_counts']; |
|
1280 |
$this->assertEqual($flag_get_user_flag_counts, 1, "hook_entity_insert() gets a correct flag count from flag_get_user_flag_counts()."); |
|
1281 |
|
|
1282 |
// Check the parameters received by hook_flag_flag(). |
|
1283 |
// Param 0: $flag. |
|
1284 |
$this->assertEqual($hook_data_variable['hook_flag_flag']['parameters'][0], $flag, "The flag object is passed to hook_flag_flag()."); |
|
1285 |
// Param 1: $entity_id. |
|
1286 |
$this->assertEqual($hook_data_variable['hook_flag_flag']['parameters'][1], $node->nid, "The entity ID is passed to hook_flag_flag()."); |
|
1287 |
// Param 2: $account. |
|
1288 |
$this->assertEqual($hook_data_variable['hook_flag_flag']['parameters'][2]->uid, $this->flag_unflag_user->uid, "The user account is passed to hook_flag_flag()."); |
|
1289 |
// Param 3: $flagging. |
|
1290 |
// See https://drupal.org/node/2196055. |
|
1291 |
$this->assertEqual($hook_data_variable['hook_flag_flag']['parameters'][3]->flag_name, $flag->name, "The Flagging entity passed to hook_flag_flag() has the expected flag name property."); |
|
1292 |
//$this->assertEqual($hook_data_variable['hook_entity_insert']['parameters'][3]->fid, $flag->fid); |
|
1293 |
//$this->assertEqual($hook_data_variable['hook_entity_insert']['parameters'][3]->entity_type, 'node'); |
|
1294 |
$this->assertEqual($hook_data_variable['hook_flag_flag']['parameters'][3]->entity_id, $node->nid, "The Flagging entity passed to hook_flag_flag() has the expected entity ID property."); |
|
1295 |
$this->assertEqual($hook_data_variable['hook_flag_flag']['parameters'][3]->uid, $this->flag_unflag_user->uid, "The Flagging entity passed to hook_flag_flag() has the expected uid property."); |
|
1296 |
|
|
1297 |
// Check the API data available to hook_flag_flag(). |
|
1298 |
//debug($hook_data_variable['hook_flag_flag']['api_calls']); |
|
1299 |
$flag_get_entity_flags = $hook_data_variable['hook_flag_flag']['api_calls']['flag_get_entity_flags']; |
|
1300 |
$flag_get_entity_flags_uids = array_keys($flag_get_entity_flags); |
|
1301 |
$this->assertEqual($flag_get_entity_flags_uids, array($this->flag_unflag_user->uid), "hook_flag_flag() gets correct data for flagging users from flag_get_entity_flags()"); |
|
1302 |
$flag_get_entity_flags_flagging = $flag_get_entity_flags[$this->flag_unflag_user->uid]; |
|
1303 |
$this->assertEqual($flag_get_entity_flags_flagging->fid, $flag->fid, "hook_flag_flag() gets a correct fid on the Flagging obtained from flag_get_entity_flags()"); |
|
1304 |
$this->assertEqual($flag_get_entity_flags_flagging->entity_type, 'node', "hook_flag_flag() gets a correct entity type on the Flagging obtained from flag_get_entity_flags()"); |
|
1305 |
$this->assertEqual($flag_get_entity_flags_flagging->entity_id, $node->nid, "hook_flag_flag() gets a correct entity ID on the Flagging obtained from flag_get_entity_flags()"); |
|
1306 |
|
|
1307 |
$flag_get_user_flags = $hook_data_variable['hook_flag_flag']['api_calls']['flag_get_user_flags']; |
|
1308 |
$flag_get_user_flags_flagging = $flag_get_user_flags[$flag->name]; |
|
1309 |
$this->assertEqual($flag_get_user_flags_flagging->fid, $flag->fid, "hook_flag_flag() gets a correct fid on the Flagging obtained from flag_get_user_flags()"); |
|
1310 |
$this->assertEqual($flag_get_user_flags_flagging->entity_type, 'node', "hook_flag_flag() gets a correct entity type on the Flagging obtained from flag_get_user_flags()"); |
|
1311 |
$this->assertEqual($flag_get_user_flags_flagging->entity_id, $node->nid, "hook_flag_flag() gets a correct entity ID on the Flagging obtained from flag_get_user_flags()"); |
|
1312 |
|
|
1313 |
// The flag counts have been increased. |
|
1314 |
$flag_get_counts = $hook_data_variable['hook_flag_flag']['api_calls']['flag_get_counts']; |
|
1315 |
$this->assertEqual($flag_get_counts[$flag->name], 1, "hook_flag_flag() gets a correct flag count from flag_get_counts()."); |
|
1316 |
|
|
1317 |
$flag_get_flag_counts = $hook_data_variable['hook_flag_flag']['api_calls']['flag_get_flag_counts']; |
|
1318 |
$this->assertEqual($flag_get_flag_counts, 1, "hook_flag_flag() gets a correct flag count from flag_get_flag_counts()."); |
|
1319 |
|
|
1320 |
$flag_get_entity_flag_counts = $hook_data_variable['hook_flag_flag']['api_calls']['flag_get_entity_flag_counts']; |
|
1321 |
$this->assertEqual($flag_get_entity_flag_counts, 1, "hook_flag_flag() gets a correct flag count from flag_get_entity_flag_counts()."); |
|
1322 |
|
|
1323 |
$flag_get_user_flag_counts = $hook_data_variable['hook_flag_flag']['api_calls']['flag_get_user_flag_counts']; |
|
1324 |
$this->assertEqual($flag_get_user_flag_counts, 1, "hook_flag_flag() gets a correct flag count from flag_get_user_flag_counts()."); |
|
1325 |
|
|
1326 |
// Clear the tracking variable. |
|
1327 |
variable_set('flag_hook_test_hook_tracking', array()); |
|
1328 |
|
|
1329 |
// Get the Flagging, and re-flag the node. |
|
1330 |
// This does nothing in our case, but is the API for updating a Flagging |
|
1331 |
// entity with changes to its Field API fields. |
|
1332 |
// Query the database to get the Flagging ID rather than Flag API so we |
|
1333 |
// don't interefere with any caches. |
|
1334 |
$query = db_select('flagging', 'f'); |
|
1335 |
$query->addField('f', 'flagging_id'); |
|
1336 |
$query->condition('fid', $flag->fid) |
|
1337 |
->condition('entity_id', $node->nid); |
|
1338 |
$flagging_id = $query |
|
1339 |
->execute() |
|
1340 |
->fetchField(); |
|
1341 |
$flagging = flagging_load($flagging_id); |
|
1342 |
|
|
1343 |
// Re-flag the node passing in the flagging entity. |
|
1344 |
$flag->flag('flag', $node->nid, $this->flag_unflag_user, FALSE, $flagging); |
|
1345 |
|
|
1346 |
// Get the variable the test module sets the hook order into. |
|
1347 |
$hook_data_variable = variable_get('flag_hook_test_hook_tracking', array()); |
|
1348 |
//debug($hook_data_variable); |
|
1349 |
|
|
1350 |
$expected_hook_order = array( |
|
1351 |
'hook_entity_presave', |
|
1352 |
'hook_entity_update', |
|
1353 |
// Note that hook_flag() and the rule are not invoked, as this is not a |
|
1354 |
// new act of flagging. |
|
1355 |
); |
|
1356 |
|
|
1357 |
// Check the hooks are invoked in the correct order. |
|
1358 |
$this->assertIdentical($expected_hook_order, array_keys($hook_data_variable), "The hooks are invoked in the correct order when re-flagging a node."); |
|
1359 |
|
|
1360 |
// Check the parameters received by hook_entity_presave(). |
|
1361 |
// Param 0: $flagging. |
|
1362 |
// There is a bug with what is passed to entity hooks; for now, just check |
|
1363 |
// the things that do get passed. |
|
1364 |
// See https://drupal.org/node/2196055. |
|
1365 |
$this->assertEqual($hook_data_variable['hook_entity_presave']['parameters'][0]->flag_name, $flag->name, "The Flagging entity passed to hook_entity_presave() has the expected flag name property."); |
|
1366 |
//$this->assertEqual($hook_data_variable['hook_entity_insert']['parameters'][0]->fid, $flag->fid); |
|
1367 |
//$this->assertEqual($hook_data_variable['hook_entity_insert']['parameters'][0]->entity_type, 'node'); |
|
1368 |
$this->assertEqual($hook_data_variable['hook_entity_presave']['parameters'][0]->entity_id, $node->nid, "The Flagging entity passed to hook_entity_presave() has the expected entity ID property."); |
|
1369 |
$this->assertEqual($hook_data_variable['hook_entity_presave']['parameters'][0]->uid, $this->flag_unflag_user->uid, "The Flagging entity passed to hook_entity_presave() has the expected uid property."); |
|
1370 |
// Param 1: $entity_type. |
|
1371 |
$this->assertEqual($hook_data_variable['hook_entity_presave']['parameters'][1], 'flagging', "hook_entity_presave() is passed the correct entity type."); |
|
1372 |
|
|
1373 |
// Check the API data available to hook_entity_presave(). |
|
1374 |
//debug($hook_data_variable['hook_entity_presave']['api_calls']); |
|
1375 |
$flag_get_entity_flags = $hook_data_variable['hook_entity_presave']['api_calls']['flag_get_entity_flags']; |
|
1376 |
$flag_get_entity_flags_uids = array_keys($flag_get_entity_flags); |
|
1377 |
$this->assertEqual($flag_get_entity_flags_uids, array($this->flag_unflag_user->uid), "hook_entity_presave() gets correct data for flagging users from flag_get_entity_flags()"); |
|
1378 |
$flag_get_entity_flags_flagging = $flag_get_entity_flags[$this->flag_unflag_user->uid]; |
|
1379 |
$this->assertEqual($flag_get_entity_flags_flagging->fid, $flag->fid, "hook_entity_presave() gets a correct fid on the Flagging obtained from flag_get_entity_flags()"); |
|
1380 |
$this->assertEqual($flag_get_entity_flags_flagging->entity_type, 'node', "hook_entity_presave() gets a correct entity type on the Flagging obtained from flag_get_entity_flags()"); |
|
1381 |
$this->assertEqual($flag_get_entity_flags_flagging->entity_id, $node->nid, "hook_entity_presave() gets a correct entity ID on the Flagging obtained from flag_get_entity_flags()"); |
|
1382 |
|
|
1383 |
$flag_get_user_flags = $hook_data_variable['hook_entity_presave']['api_calls']['flag_get_user_flags']; |
|
1384 |
$flag_get_user_flags_flagging = $flag_get_user_flags[$flag->name]; |
|
1385 |
$this->assertEqual($flag_get_user_flags_flagging->fid, $flag->fid, "hook_entity_presave() gets a correct fid on the Flagging obtained from flag_get_user_flags()"); |
|
1386 |
$this->assertEqual($flag_get_user_flags_flagging->entity_type, 'node', "hook_entity_presave() gets a correct entity type on the Flagging obtained from flag_get_user_flags()"); |
|
1387 |
$this->assertEqual($flag_get_user_flags_flagging->entity_id, $node->nid, "hook_entity_presave() gets a correct entity ID on the Flagging obtained from flag_get_user_flags()"); |
|
1388 |
|
|
1389 |
// The flag counts have not changed. |
|
1390 |
$flag_get_counts = $hook_data_variable['hook_entity_presave']['api_calls']['flag_get_counts']; |
|
1391 |
$this->assertEqual($flag_get_counts[$flag->name], 1, "hook_entity_presave() gets a correct flag count from flag_get_counts()."); |
|
1392 |
|
|
1393 |
$flag_get_flag_counts = $hook_data_variable['hook_entity_presave']['api_calls']['flag_get_flag_counts']; |
|
1394 |
$this->assertEqual($flag_get_flag_counts, 1, "hook_entity_presave() gets a correct flag count from flag_get_flag_counts()."); |
|
1395 |
|
|
1396 |
$flag_get_entity_flag_counts = $hook_data_variable['hook_entity_presave']['api_calls']['flag_get_entity_flag_counts']; |
|
1397 |
$this->assertEqual($flag_get_entity_flag_counts, 1, "hook_entity_presave() gets a correct flag count from flag_get_entity_flag_counts()."); |
|
1398 |
|
|
1399 |
$flag_get_user_flag_counts = $hook_data_variable['hook_entity_presave']['api_calls']['flag_get_user_flag_counts']; |
|
1400 |
$this->assertEqual($flag_get_user_flag_counts, 1, "hook_entity_presave() gets a correct flag count from flag_get_user_flag_counts()."); |
|
1401 |
|
|
1402 |
// Check the parameters received by hook_entity_update(). |
|
1403 |
// Param 0: $flagging. |
|
1404 |
// See https://drupal.org/node/2196055. |
|
1405 |
$this->assertEqual($hook_data_variable['hook_entity_update']['parameters'][0]->flag_name, $flag->name, "The Flagging entity passed to hook_entity_update() has the expected flag name property."); |
|
1406 |
//$this->assertEqual($hook_data_variable['hook_entity_update']['parameters'][0]->fid, $flag->fid); |
|
1407 |
//$this->assertEqual($hook_data_variable['hook_entity_update']['parameters'][0]->entity_type, 'node'); |
|
1408 |
$this->assertEqual($hook_data_variable['hook_entity_update']['parameters'][0]->entity_id, $node->nid, "The Flagging entity passed to hook_entity_update() has the expected entity ID property."); |
|
1409 |
$this->assertEqual($hook_data_variable['hook_entity_update']['parameters'][0]->uid, $this->flag_unflag_user->uid, "The Flagging entity passed to hook_entity_update() has the expected uid property."); |
|
1410 |
$this->assertTrue(!empty($hook_data_variable['hook_entity_update']['parameters'][0]->flagging_id), "The Flagging entity passed to hook_entity_update() has the flagging ID set."); |
|
1411 |
// Param 1: $entity_type. |
|
1412 |
$this->assertEqual($hook_data_variable['hook_entity_update']['parameters'][1], 'flagging', "hook_entity_update() is passed the correct entity type."); |
|
1413 |
|
|
1414 |
// Check the API data available to hook_entity_update(). |
|
1415 |
//debug($hook_data_variable['hook_entity_update']['api_calls']); |
|
1416 |
$flag_get_entity_flags = $hook_data_variable['hook_entity_update']['api_calls']['flag_get_entity_flags']; |
|
1417 |
$flag_get_entity_flags_uids = array_keys($flag_get_entity_flags); |
|
1418 |
$this->assertEqual($flag_get_entity_flags_uids, array($this->flag_unflag_user->uid), "hook_entity_update() gets correct data for flagging users from flag_get_entity_flags()"); |
|
1419 |
$flag_get_entity_flags_flagging = $flag_get_entity_flags[$this->flag_unflag_user->uid]; |
|
1420 |
$this->assertEqual($flag_get_entity_flags_flagging->fid, $flag->fid, "hook_entity_update() gets a correct fid on the Flagging obtained from flag_get_entity_flags()"); |
|
1421 |
$this->assertEqual($flag_get_entity_flags_flagging->entity_type, 'node', "hook_entity_update() gets a correct entity type on the Flagging obtained from flag_get_entity_flags()"); |
|
1422 |
$this->assertEqual($flag_get_entity_flags_flagging->entity_id, $node->nid, "hook_entity_update() gets a correct entity ID on the Flagging obtained from flag_get_entity_flags()"); |
|
1423 |
|
|
1424 |
$flag_get_user_flags = $hook_data_variable['hook_entity_update']['api_calls']['flag_get_user_flags']; |
|
1425 |
$flag_get_user_flags_flagging = $flag_get_user_flags[$flag->name]; |
|
1426 |
$this->assertEqual($flag_get_user_flags_flagging->fid, $flag->fid, "hook_entity_update() gets a correct fid on the Flagging obtained from flag_get_user_flags()"); |
|
1427 |
$this->assertEqual($flag_get_user_flags_flagging->entity_type, 'node', "hook_entity_update() gets a correct entity type on the Flagging obtained from flag_get_user_flags()"); |
|
1428 |
$this->assertEqual($flag_get_user_flags_flagging->entity_id, $node->nid, "hook_entity_update() gets a correct entity ID on the Flagging obtained from flag_get_user_flags()"); |
|
1429 |
|
|
1430 |
// The flag counts have not changed. |
|
1431 |
$flag_get_counts = $hook_data_variable['hook_entity_update']['api_calls']['flag_get_counts']; |
|
1432 |
$this->assertEqual($flag_get_counts[$flag->name], 1, "hook_entity_update() gets a correct flag count from flag_get_counts()."); |
|
1433 |
|
|
1434 |
$flag_get_flag_counts = $hook_data_variable['hook_entity_update']['api_calls']['flag_get_flag_counts']; |
|
1435 |
$this->assertEqual($flag_get_flag_counts, 1, "hook_entity_update() gets a correct flag count from flag_get_flag_counts()."); |
|
1436 |
|
|
1437 |
$flag_get_entity_flag_counts = $hook_data_variable['hook_entity_update']['api_calls']['flag_get_entity_flag_counts']; |
|
1438 |
$this->assertEqual($flag_get_entity_flag_counts, 1, "hook_entity_update() gets a correct flag count from flag_get_entity_flag_counts()."); |
|
1439 |
|
|
1440 |
$flag_get_user_flag_counts = $hook_data_variable['hook_entity_update']['api_calls']['flag_get_user_flag_counts']; |
|
1441 |
$this->assertEqual($flag_get_user_flag_counts, 1, "hook_entity_update() gets a correct flag count from flag_get_user_flag_counts()."); |
|
1442 |
|
|
1443 |
// Clear the tracking variable. |
|
1444 |
variable_set('flag_hook_test_hook_tracking', array()); |
|
1445 |
|
|
1446 |
// Unflag the node as the user. |
|
1447 |
$flag->flag('unflag', $node->nid, $this->flag_unflag_user); |
|
1448 |
|
|
1449 |
// Get the variable the test module sets the hook order into. |
|
1450 |
$hook_data_variable = variable_get('flag_hook_test_hook_tracking', array()); |
|
1451 |
//debug($hook_data_variable); |
|
1452 |
|
|
1453 |
$expected_hook_order = array( |
|
1454 |
'hook_flag_unflag', |
|
1455 |
'rules_event', |
|
1456 |
'hook_entity_delete', |
|
1457 |
); |
|
1458 |
|
|
1459 |
// Check the hooks are invoked in the correct order. |
|
1460 |
$this->assertIdentical($expected_hook_order, array_keys($hook_data_variable), "The hooks are invoked in the correct order when unflagging a node."); |
|
1461 |
|
|
1462 |
// Check the parameters received by hook_flag_unflag(). |
|
1463 |
// Param 0: $flag. |
|
1464 |
$this->assertEqual($hook_data_variable['hook_flag_unflag']['parameters'][0], $flag, "The flag object is passed to hook_flag_unflag()."); |
|
1465 |
// Param 1: $entity_id. |
|
1466 |
$this->assertEqual($hook_data_variable['hook_flag_unflag']['parameters'][1], $node->nid, "The entity ID is passed to hook_flag_unflag()."); |
|
1467 |
// Param 2: $account. |
|
1468 |
$this->assertEqual($hook_data_variable['hook_flag_unflag']['parameters'][2]->uid, $this->flag_unflag_user->uid, "The user account is passed to hook_flag_unflag()."); |
|
1469 |
// Param 3: $flagging. |
|
1470 |
// See https://drupal.org/node/2196055. |
|
1471 |
$this->assertEqual($hook_data_variable['hook_flag_unflag']['parameters'][3]->flag_name, $flag->name, "The Flagging entity passed to hook_flag_unflag() has the expected flag name property."); |
|
1472 |
//$this->assertEqual($hook_data_variable['hook_entity_insert']['parameters'][3]->fid, $flag->fid); |
|
1473 |
//$this->assertEqual($hook_data_variable['hook_entity_insert']['parameters'][3]->entity_type, 'node'); |
|
1474 |
$this->assertEqual($hook_data_variable['hook_flag_unflag']['parameters'][3]->entity_id, $node->nid, "The Flagging entity passed to hook_flag_unflag() has the expected entity ID property."); |
|
1475 |
$this->assertEqual($hook_data_variable['hook_flag_unflag']['parameters'][3]->uid, $this->flag_unflag_user->uid, "The Flagging entity passed to hook_flag_unflag() has the expected uid property."); |
|
1476 |
|
|
1477 |
// Check the API data available to hook_flag_unflag(). |
|
1478 |
//debug($hook_data_variable['hook_flag_unflag']['api_calls']); |
|
1479 |
// The unflagging is not yet done, so flag_get_entity_flags() will find the |
|
1480 |
// flagging data. |
|
1481 |
$flag_get_entity_flags = $hook_data_variable['hook_flag_unflag']['api_calls']['flag_get_entity_flags']; |
|
1482 |
$flag_get_entity_flags_uids = array_keys($flag_get_entity_flags); |
|
1483 |
$this->assertEqual($flag_get_entity_flags_uids, array($this->flag_unflag_user->uid), "hook_flag_unflag() gets correct data for flagging users from flag_get_entity_flags()"); |
|
1484 |
$flag_get_entity_flags_flagging = $flag_get_entity_flags[$this->flag_unflag_user->uid]; |
|
1485 |
$this->assertEqual($flag_get_entity_flags_flagging->fid, $flag->fid, "hook_flag_unflag() gets a correct fid on the Flagging obtained from flag_get_entity_flags(): the Flagging has not yet been deleted."); |
|
1486 |
$this->assertEqual($flag_get_entity_flags_flagging->entity_type, 'node', "hook_flag_unflag() gets a correct entity type on the Flagging obtained from flag_get_entity_flags()"); |
|
1487 |
$this->assertEqual($flag_get_entity_flags_flagging->entity_id, $node->nid, "hook_flag_unflag() gets a correct entity ID on the Flagging obtained from flag_get_entity_flags()"); |
|
1488 |
|
|
1489 |
$flag_get_user_flags = $hook_data_variable['hook_flag_unflag']['api_calls']['flag_get_user_flags']; |
|
1490 |
$flag_get_user_flags_flagging = $flag_get_user_flags[$flag->name]; |
|
1491 |
$this->assertEqual($flag_get_user_flags_flagging->fid, $flag->fid, "hook_flag_unflag() gets a correct fid on the Flagging obtained from flag_get_user_flags()"); |
|
1492 |
$this->assertEqual($flag_get_user_flags_flagging->entity_type, 'node', "hook_flag_unflag() gets a correct entity type on the Flagging obtained from flag_get_user_flags()"); |
|
1493 |
$this->assertEqual($flag_get_user_flags_flagging->entity_id, $node->nid, "hook_flag_unflag() gets a correct entity ID on the Flagging obtained from flag_get_user_flags()"); |
|
1494 |
|
|
1495 |
// The flag counts have already been decreased. |
|
1496 |
$flag_get_counts = $hook_data_variable['hook_flag_unflag']['api_calls']['flag_get_counts']; |
|
1497 |
$this->assertEqual($flag_get_counts, array(), "hook_flag_unflag() gets a correct flag count from flag_get_counts()."); |
|
1498 |
|
|
1499 |
$flag_get_flag_counts = $hook_data_variable['hook_flag_unflag']['api_calls']['flag_get_flag_counts']; |
|
1500 |
$this->assertEqual($flag_get_flag_counts, 0, "hook_flag_unflag() gets a correct flag count from flag_get_flag_counts()."); |
|
1501 |
|
|
1502 |
// flag_get_entity_flag_counts() queries the {flagging} table, so is not |
|
1503 |
// updated yet. |
|
1504 |
$flag_get_entity_flag_counts = $hook_data_variable['hook_flag_unflag']['api_calls']['flag_get_entity_flag_counts']; |
|
1505 |
$this->assertEqual($flag_get_entity_flag_counts, 1, "hook_flag_unflag() gets a correct flag count from flag_get_entity_flag_counts()."); |
|
1506 |
|
|
1507 |
// flag_get_user_flag_counts() queries the {flagging} table, so is not |
|
1508 |
// updated yet. |
|
1509 |
$flag_get_user_flag_counts = $hook_data_variable['hook_flag_unflag']['api_calls']['flag_get_user_flag_counts']; |
|
1510 |
$this->assertEqual($flag_get_user_flag_counts, 1, "hook_flag_unflag() gets a correct flag count from flag_get_user_flag_counts()."); |
|
1511 |
|
|
1512 |
// Check the parameters received by hook_entity_delete(). |
|
1513 |
// Param 0: $flagging. |
|
1514 |
// See https://drupal.org/node/2196055. |
|
1515 |
$this->assertEqual($hook_data_variable['hook_entity_delete']['parameters'][0]->flag_name, $flag->name, "The Flagging entity passed to hook_entity_delete() has the expected flag name property."); |
|
1516 |
//$this->assertEqual($hook_data_variable['hook_entity_delete']['parameters'][0]->fid, $flag->fid); |
|
1517 |
//$this->assertEqual($hook_data_variable['hook_entity_delete']['parameters'][0]->entity_type, 'node'); |
|
1518 |
$this->assertEqual($hook_data_variable['hook_entity_delete']['parameters'][0]->entity_id, $node->nid, "The Flagging entity passed to hook_entity_delete() has the expected entity ID property."); |
|
1519 |
$this->assertEqual($hook_data_variable['hook_entity_delete']['parameters'][0]->uid, $this->flag_unflag_user->uid, "The Flagging entity passed to hook_entity_delete() has the expected uid property."); |
|
1520 |
$this->assertTrue(!empty($hook_data_variable['hook_entity_delete']['parameters'][0]->flagging_id), "The Flagging entity passed to hook_entity_delete() has the flagging ID set."); |
|
1521 |
// Param 1: $entity_type. |
|
1522 |
$this->assertEqual($hook_data_variable['hook_entity_delete']['parameters'][1], 'flagging', "hook_entity_delete() is passed the correct entity type."); |
|
1523 |
|
|
1524 |
// Check the API data available to hook_entity_delete(). |
|
1525 |
//debug($hook_data_variable['hook_entity_delete']['api_calls']); |
|
1526 |
// The unflagging is not yet done, so hook_entity_delete() will find the |
|
1527 |
// flagging data. |
|
1528 |
$flag_get_entity_flags = $hook_data_variable['hook_entity_delete']['api_calls']['flag_get_entity_flags']; |
|
1529 |
$flag_get_entity_flags_uids = array_keys($flag_get_entity_flags); |
|
1530 |
$this->assertEqual($flag_get_entity_flags_uids, array($this->flag_unflag_user->uid), "hook_entity_delete() gets correct data for flagging users from flag_get_entity_flags()"); |
|
1531 |
$flag_get_entity_flags_flagging = $flag_get_entity_flags[$this->flag_unflag_user->uid]; |
|
1532 |
$this->assertEqual($flag_get_entity_flags_flagging->fid, $flag->fid, "hook_entity_delete() gets a correct fid on the Flagging obtained from flag_get_entity_flags()"); |
|
1533 |
$this->assertEqual($flag_get_entity_flags_flagging->entity_type, 'node', "hook_entity_delete() gets a correct entity type on the Flagging obtained from flag_get_entity_flags()"); |
|
1534 |
$this->assertEqual($flag_get_entity_flags_flagging->entity_id, $node->nid, "hook_entity_delete() gets a correct entity ID on the Flagging obtained from flag_get_entity_flags()"); |
|
1535 |
|
|
1536 |
$flag_get_user_flags = $hook_data_variable['hook_entity_delete']['api_calls']['flag_get_user_flags']; |
|
1537 |
$flag_get_user_flags_flagging = $flag_get_user_flags[$flag->name]; |
|
1538 |
$this->assertEqual($flag_get_user_flags_flagging->fid, $flag->fid, "hook_entity_delete() gets a correct fid on the Flagging obtained from flag_get_user_flags()"); |
|
1539 |
$this->assertEqual($flag_get_user_flags_flagging->entity_type, 'node', "hook_entity_delete() gets a correct entity type on the Flagging obtained from flag_get_user_flags()"); |
|
1540 |
$this->assertEqual($flag_get_user_flags_flagging->entity_id, $node->nid, "hook_entity_delete() gets a correct entity ID on the Flagging obtained from flag_get_user_flags()"); |
|
1541 |
|
|
1542 |
// The flag counts have already been decreased. |
|
1543 |
$flag_get_counts = $hook_data_variable['hook_entity_delete']['api_calls']['flag_get_counts']; |
|
1544 |
$this->assertEqual($flag_get_counts, array(), "hook_entity_delete() gets a correct flag count from flag_get_counts()."); |
|
1545 |
|
|
1546 |
$flag_get_flag_counts = $hook_data_variable['hook_entity_delete']['api_calls']['flag_get_flag_counts']; |
|
1547 |
$this->assertEqual($flag_get_flag_counts, 0, "hook_entity_delete() gets a correct flag count from flag_get_flag_counts()."); |
|
1548 |
|
|
1549 |
// flag_get_entity_flag_counts() queries the {flagging} table, so is not |
|
1550 |
// updated yet. |
|
1551 |
$flag_get_entity_flag_counts = $hook_data_variable['hook_entity_delete']['api_calls']['flag_get_entity_flag_counts']; |
|
1552 |
$this->assertEqual($flag_get_entity_flag_counts, 1, "hook_entity_delete() gets a correct flag count from flag_get_entity_flag_counts()."); |
|
1553 |
|
|
1554 |
// flag_get_user_flag_counts() queries the {flagging} table, so is not |
|
1555 |
// updated yet. |
|
1556 |
$flag_get_user_flag_counts = $hook_data_variable['hook_entity_delete']['api_calls']['flag_get_user_flag_counts']; |
|
1557 |
$this->assertEqual($flag_get_user_flag_counts, 1, "hook_entity_delete() gets a correct flag count from flag_get_user_flag_counts()."); |
|
1558 |
} |
|
1559 |
|
|
1560 |
} |
drupal7/sites/all/modules/flag/tests/flag_hook_test/flag_hook_test.info | ||
---|---|---|
1 |
name = Flag Hooks Test |
|
2 |
description = Test module for invocation of flag hooks. |
|
3 |
dependencies[] = flag |
|
4 |
dependencies[] = rules |
|
5 |
core = 7.x |
|
6 |
hidden = TRUE |
|
7 |
|
|
8 |
; Information added by Drupal.org packaging script on 2014-03-18 |
|
9 |
version = "7.x-3.4" |
|
10 |
core = "7.x" |
|
11 |
project = "flag" |
|
12 |
datestamp = "1395128956" |
|
13 |
|
drupal7/sites/all/modules/flag/tests/flag_hook_test/flag_hook_test.module | ||
---|---|---|
1 |
<?php |
|
2 |
|
|
3 |
/** |
|
4 |
* @file flag_hook_test.module |
|
5 |
* Test module for the hooks that Flag invokes. |
|
6 |
*/ |
|
7 |
|
|
8 |
/** |
|
9 |
* Store the hook name and parameters into a variable for retrieval by the test. |
|
10 |
* |
|
11 |
* Hook implementations should call this with their hook name and parameters. |
|
12 |
* |
|
13 |
* @param $hook_name |
|
14 |
* The name of the hook invoked. |
|
15 |
* @param $function_parameters |
|
16 |
* The array of parameters the hook received. |
|
17 |
* @param $flagging |
|
18 |
* (optional) The flagging entity that the hook received. If this is given, |
|
19 |
* then various flag API functions have their data set into the tracking |
|
20 |
* variable for verification by the test case. |
|
21 |
*/ |
|
22 |
function _flag_hook_test_record_invocation($hook_name, $function_parameters, $flagging = NULL) { |
|
23 |
$variable = variable_get('flag_hook_test_hook_tracking', array()); |
|
24 |
|
|
25 |
$variable[$hook_name] = array(); |
|
26 |
$variable[$hook_name]['parameters'] = $function_parameters; |
|
27 |
|
|
28 |
// If a Flagging entity was passed in, call API functions and store their data |
|
29 |
// for the test case to check. |
|
30 |
if (isset($flagging)) { |
|
31 |
$flag = flag_get_flag(NULL, $flagging->fid); |
|
32 |
|
|
33 |
$variable[$hook_name]['api_calls'] = array(); |
|
34 |
|
|
35 |
$variable[$hook_name]['api_calls']['flag_get_entity_flags'] = flag_get_entity_flags('node', $flagging->entity_id, $flag->name); |
|
36 |
|
|
37 |
$variable[$hook_name]['api_calls']['flag_get_user_flags'] = flag_get_user_flags('node', $flagging->entity_id, $flagging->uid); |
|
38 |
|
|
39 |
$variable[$hook_name]['api_calls']['flag_get_counts'] = flag_get_counts('node', $flagging->entity_id); |
|
40 |
|
|
41 |
$variable[$hook_name]['api_calls']['flag_get_flag_counts'] = flag_get_flag_counts($flag->name); |
|
42 |
|
|
43 |
$variable[$hook_name]['api_calls']['flag_get_entity_flag_counts'] = flag_get_entity_flag_counts($flag, 'node'); |
|
44 |
|
|
45 |
$account = user_load($flagging->uid); |
|
46 |
$variable[$hook_name]['api_calls']['flag_get_user_flag_counts'] = flag_get_user_flag_counts($flag, $account); |
|
47 |
} |
|
48 |
|
|
49 |
variable_set('flag_hook_test_hook_tracking', $variable); |
|
50 |
} |
|
51 |
|
Formats disponibles : Unified diff
Weekly update of contrib modules