-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_contentrules.py
617 lines (516 loc) · 19.5 KB
/
test_contentrules.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
# -*- coding: utf-8 -*-
from collective.contentalerts.contentrules import ForbiddenTextAlertCondition
from collective.contentalerts.contentrules import InadequateTextAlertCondition
from collective.contentalerts.contentrules import TextAlertCondition
from collective.contentalerts.contentrules import TextAlertConditionEditFormView # noqa
from collective.contentalerts.interfaces import IAlert
from collective.contentalerts.interfaces import IHasStopWords
from collective.contentalerts.interfaces import IStopWords
from collective.contentalerts.testing import COLLECTIVE_CONTENTALERTS_INTEGRATION_TESTING # noqa
from plone import api
from plone.app.contentrules.rule import Rule
from plone.app.discussion.interfaces import IConversation
from plone.app.testing import setRoles
from plone.app.testing import TEST_USER_ID
from plone.contentrules.engine.interfaces import IRuleStorage
from plone.contentrules.rule.interfaces import IExecutable
from plone.contentrules.rule.interfaces import IRuleCondition
from plone.stringinterp.interfaces import IStringSubstitution
from Testing.ZopeTestCase.utils import setupCoreSessions
from zope.component import createObject
from zope.component import getAdapter
from zope.component import getMultiAdapter
from zope.component import getUtility
from zope.component.interfaces import IObjectEvent
from zope.interface import implementer
import unittest
@implementer(IObjectEvent)
class CommentDummyEvent(object):
def __init__(self, obj):
self.comment = obj
@implementer(IObjectEvent)
class ContentTypeDummyEvent(object):
def __init__(self, obj):
self.object = obj
class TextAlertConditionTestCase(unittest.TestCase):
layer = COLLECTIVE_CONTENTALERTS_INTEGRATION_TESTING
def setUp(self):
self.portal = self.layer['portal']
self.request = self.layer['request']
setRoles(self.portal, TEST_USER_ID, ['Manager'])
self.name = 'collective.contentalerts.TextAlert'
self.element = getUtility(
IRuleCondition,
name=self.name
)
self.document = api.content.create(
container=self.portal,
id='doc1',
title='Document 1',
type='Document'
)
def _add_comment(self, text):
comment = createObject('plone.Comment')
comment.text = text
comment.author_username = 'jim'
comment.author_name = 'Jim'
comment.author_email = 'jim@example.com'
conversation = IConversation(self.document)
conversation.addComment(comment)
return comment
def _set_record_value(self, value, record='inadequate_words'):
api.portal.set_registry_record(
name=record,
interface=IStopWords,
value=value
)
def test_registered(self):
self.assertEqual(self.name, self.element.addview)
self.assertEqual('edit', self.element.editview)
self.assertEqual(None, self.element.for_)
self.assertEqual(IObjectEvent, self.element.event)
def test_add_view_no_data(self):
storage = getUtility(IRuleStorage)
storage[u'foo'] = Rule()
rule = self.portal.restrictedTraverse('++rule++foo')
adding = getMultiAdapter(
(rule, self.portal.REQUEST),
name='+condition'
)
add_view = getMultiAdapter(
(adding, self.portal.REQUEST),
name=self.element.addview
)
add_view.form_instance.update()
content = add_view.form_instance.create(data={})
add_view.form_instance.add(content)
condition = rule.conditions[0]
self.assertTrue(isinstance(condition, TextAlertCondition))
self.assertEqual(condition.stop_words, None)
def test_add_view_with_stop_words(self):
stop_words = u'alert\nanother alert\nlast one'
storage = getUtility(IRuleStorage)
storage[u'foo'] = Rule()
rule = self.portal.restrictedTraverse('++rule++foo')
adding = getMultiAdapter(
(rule, self.portal.REQUEST),
name='+condition'
)
add_view = getMultiAdapter(
(adding, self.portal.REQUEST),
name=self.element.addview
)
add_view.form_instance.update()
content = add_view.form_instance.create(
data={'stop_words': stop_words}
)
add_view.form_instance.add(content)
condition = rule.conditions[0]
self.assertTrue(isinstance(condition, TextAlertCondition))
self.assertEqual(
condition.stop_words,
stop_words
)
def test_edit_view(self):
condition = TextAlertCondition()
edit_view = getMultiAdapter(
(condition, self.request),
name=self.element.editview
)
self.assertTrue(
isinstance(edit_view, TextAlertConditionEditFormView)
)
def test_empty_text_no_condition(self):
comment = self._add_comment('')
condition = TextAlertCondition()
executable = getMultiAdapter(
(self.portal, condition, CommentDummyEvent(comment)),
IExecutable
)
self.assertFalse(executable())
def test_no_text_no_condition(self):
comment = self._add_comment(None)
condition = TextAlertCondition()
executable = getMultiAdapter(
(self.portal, condition, CommentDummyEvent(comment)),
IExecutable
)
self.assertFalse(executable())
def test_regular_text_no_local_no_registry_stop_words(self):
comment = self._add_comment('regular text')
condition = TextAlertCondition()
self.assertEqual(
getUtility(IAlert)._get_registry_stop_words(),
''
)
self.assertEqual(condition.stop_words, None)
executable = getMultiAdapter(
(self.portal, condition, CommentDummyEvent(comment)),
IExecutable
)
self.assertFalse(executable())
def test_regular_text_no_local_stop_words_and_registry_stop_words(self):
comment = self._add_comment('regular text')
condition = TextAlertCondition()
self.assertEqual(
getUtility(IAlert)._get_registry_stop_words(),
''
)
condition.stop_words = u'one alert\nanother alert'
executable = getMultiAdapter(
(self.portal, condition, CommentDummyEvent(comment)),
IExecutable
)
self.assertFalse(executable())
def test_regular_text_local_stop_words_and_no_registry_stop_words(self):
comment = self._add_comment('regular text')
condition = TextAlertCondition()
self._set_record_value(u'one alert\nanother alert')
self.assertEqual(condition.stop_words, None)
executable = getMultiAdapter(
(self.portal, condition, CommentDummyEvent(comment)),
IExecutable
)
self.assertFalse(executable())
def test_regular_text_local_and_registry_stop_words(self):
comment = self._add_comment('regular text')
condition = TextAlertCondition()
self._set_record_value(u'yet another\nlast one')
condition.stop_words = u'one alert\nanother alert'
executable = getMultiAdapter(
(self.portal, condition, CommentDummyEvent(comment)),
IExecutable
)
self.assertFalse(executable())
def test_alert_text_no_local_stop_words_and_registry_stop_words(self):
comment = self._add_comment('this gives one alert')
condition = TextAlertCondition()
self._set_record_value(u'one alert\nanother alert')
self.assertEqual(condition.stop_words, None)
executable = getMultiAdapter(
(self.portal, condition, CommentDummyEvent(comment)),
IExecutable
)
self.assertTrue(executable())
def test_alert_text_local_stop_words_no_registry_stop_words(self):
comment = self._add_comment('this gives one alert')
condition = TextAlertCondition()
self.assertEqual(
getUtility(IAlert)._get_registry_stop_words(),
''
)
condition.stop_words = u'one alert\nanother alert'
executable = getMultiAdapter(
(self.portal, condition, CommentDummyEvent(comment)),
IExecutable
)
self.assertTrue(executable())
def test_alert_text_local_and_registry_stop_words(self):
comment = self._add_comment('this gives one alert')
condition = TextAlertCondition()
self._set_record_value(u'almost\nlast one')
condition.stop_words = u'one alert\nanother alert'
executable = getMultiAdapter(
(self.portal, condition, CommentDummyEvent(comment)),
IExecutable
)
self.assertTrue(executable())
def test_alert_text_local_stop_words_shadow_registry_stop_words(self):
"""Local stop words list shadows the registry stop words.
This basically means that if the text contains stop words from
the registry, but there is a local stop words list that does not
complain, the text will be reported that it does *not* contain stop
words.
That's a way to override the general stop words list to provide a
completely different set of stop words.
"""
comment = self._add_comment('this should give one alert')
condition = TextAlertCondition()
self._set_record_value(u'one alert\nanother alert')
condition.stop_words = u'almost\nlast one'
executable = getMultiAdapter(
(self.portal, condition, CommentDummyEvent(comment)),
IExecutable
)
self.assertFalse(executable())
def test_document(self):
document = api.content.create(
container=self.portal,
id='doc2',
title='Document 2',
type='Document'
)
document.text = 'this gives one alert'
condition = TextAlertCondition()
condition.stop_words = u'one alert\nanother alert'
executable = getMultiAdapter(
(self.portal, condition, ContentTypeDummyEvent(document)),
IExecutable
)
self.assertTrue(executable())
def test_stop_words_on_request(self):
comment = self._add_comment('whatever')
condition = TextAlertCondition()
condition.stop_words = u'one alert\nanother alert'
executable = getMultiAdapter(
(self.portal, condition, CommentDummyEvent(comment)),
IExecutable
)
executable()
self.assertEqual(
self.request.get('stop_words'),
condition.stop_words
)
def test_stop_words_not_in_request(self):
comment = self._add_comment('whatever')
condition = TextAlertCondition()
executable = getMultiAdapter(
(self.portal, condition, CommentDummyEvent(comment)),
IExecutable
)
executable()
self.assertIsNone(self.request.get('stop_words'))
def test_has_stop_words_add_interface_comment(self):
comment = self._add_comment('one alert')
condition = TextAlertCondition()
condition.stop_words = u'one alert\nanother alert'
executable = getMultiAdapter(
(self.portal, condition, CommentDummyEvent(comment)),
IExecutable
)
executable()
self.assertTrue(IHasStopWords.providedBy(comment))
def test_has_stop_words_add_interface_document(self):
document = api.content.create(
container=self.portal,
id='doc2',
title='Document 2',
type='Document'
)
document.text = 'this gives one alert'
condition = TextAlertCondition()
condition.stop_words = u'one alert\nanother alert'
executable = getMultiAdapter(
(self.portal, condition, ContentTypeDummyEvent(document)),
IExecutable
)
executable()
self.assertTrue(IHasStopWords.providedBy(document))
def test_no_stop_words_no_interface(self):
comment = self._add_comment('no alert')
condition = TextAlertCondition()
condition.stop_words = u'one alert\nanother alert'
executable = getMultiAdapter(
(self.portal, condition, CommentDummyEvent(comment)),
IExecutable
)
executable()
self.assertFalse(IHasStopWords.providedBy(comment))
def test_add_and_remove_interface(self):
comment = self._add_comment('one alert')
condition = TextAlertCondition()
condition.stop_words = u'one alert\nanother alert'
# adds the marker interface
executable = getMultiAdapter(
(self.portal, condition, CommentDummyEvent(comment)),
IExecutable
)
executable()
comment.text = 'no longer creating an alert'
executable()
self.assertFalse(IHasStopWords.providedBy(comment))
def test_has_stop_words_add_interface_document_on_catalog(self):
document = api.content.create(
container=self.portal,
id='doc2',
title='Document 2',
type='Document'
)
document.text = 'this gives one alert'
condition = TextAlertCondition()
condition.stop_words = u'one alert\nanother alert'
executable = getMultiAdapter(
(self.portal, condition, ContentTypeDummyEvent(document)),
IExecutable
)
executable()
brains = api.content.find(
self.portal,
object_provides=IHasStopWords.__identifier__
)
self.assertEqual(len(brains), 1)
def test_no_stop_words_no_interface_on_catalog(self):
comment = self._add_comment('no alert')
condition = TextAlertCondition()
condition.stop_words = u'one alert\nanother alert'
executable = getMultiAdapter(
(self.portal, condition, CommentDummyEvent(comment)),
IExecutable
)
executable()
brains = api.content.find(
self.portal,
object_provides=IHasStopWords.__identifier__
)
self.assertEqual(len(brains), 0)
def test_add_and_remove_interface_on_catalog(self):
self._add_comment('one alert')
# acquisition wrapped version of the comment
comment = IConversation(self.document).values()[0]
condition = TextAlertCondition()
condition.stop_words = u'one alert\nanother alert'
# adds the marker interface
executable = getMultiAdapter(
(self.portal, condition, CommentDummyEvent(comment)),
IExecutable
)
executable()
brains = api.content.find(
self.portal,
object_provides=IHasStopWords.__identifier__
)
self.assertEqual(len(brains), 1)
comment.text = 'no longer creating an alert'
executable()
brains = api.content.find(
self.portal,
object_provides=IHasStopWords.__identifier__
)
self.assertEqual(len(brains), 0)
class SpecificAlertConditionsTestCase(unittest.TestCase):
layer = COLLECTIVE_CONTENTALERTS_INTEGRATION_TESTING
def setUp(self):
self.portal = self.layer['portal']
self.request = self.layer['request']
setRoles(self.portal, TEST_USER_ID, ['Manager'])
def _set_record_value(self, value, record='inadequate_words'):
api.portal.set_registry_record(
name=record,
interface=IStopWords,
value=value
)
def test_inadequate_condition(self):
document = api.content.create(
container=self.portal,
id='doc2',
title='Document 2',
type='Document'
)
document.text = 'this gives one alert'
condition = InadequateTextAlertCondition()
self._set_record_value(u'one')
executable = getMultiAdapter(
(self.portal, condition, ContentTypeDummyEvent(document)),
IExecutable
)
self.assertTrue(executable())
def test_forbidden_condition(self):
document = api.content.create(
container=self.portal,
id='doc2',
title='Document 2',
type='Document'
)
document.text = 'this gives one alert'
condition = ForbiddenTextAlertCondition()
self._set_record_value(u'one', record='forbidden_words')
executable = getMultiAdapter(
(self.portal, condition, ContentTypeDummyEvent(document)),
IExecutable
)
self.assertTrue(executable())
class ContentRulesSubstitutionsTest(unittest.TestCase):
layer = COLLECTIVE_CONTENTALERTS_INTEGRATION_TESTING
def setUp(self):
setupCoreSessions(self.layer['app'])
self.portal = self.layer['portal']
self.request = self.layer['request']
setRoles(self.portal, TEST_USER_ID, ['Manager'])
self.document = api.content.create(
container=self.portal,
id='doc1',
title='Document 1',
type='Document'
)
def _add_comment(self, text='lilala'):
comment = createObject('plone.Comment')
comment.text = text
comment.author_username = 'jim'
comment.author_name = 'Jim'
comment.author_email = 'jim@example.com'
conversation = IConversation(self.document)
conversation.addComment(comment)
def test_stop_words_on_request(self):
stop_words = 'hi\nI am around'
self.request.set('stop_words', stop_words)
text_alert = getAdapter(
self.document,
IStringSubstitution,
name=u'text_alert'
)
self.assertEqual(
text_alert._get_stop_words(),
stop_words
)
def test_no_stop_words_on_request(self):
text_alert = getAdapter(
self.document,
IStringSubstitution,
name=u'text_alert'
)
self.assertIsNone(text_alert._get_stop_words())
def test_get_text_from_comment(self):
text = 'some random text'
self._add_comment(text=text)
text_alert = getAdapter(
self.document,
IStringSubstitution,
name=u'comment_alert'
)
self.assertEqual(
text_alert._get_text(),
text
)
def test_no_comment_no_text(self):
text_alert = getAdapter(
self.document,
IStringSubstitution,
name=u'comment_alert'
)
self.assertEqual(
text_alert._get_text(),
u''
)
def test_get_text_from_document(self):
text = 'some random text'
self.document.text = text
text_alert = getAdapter(
self.document,
IStringSubstitution,
name=u'text_alert'
)
self.assertIn(
text,
text_alert._get_text(),
)
def test_get_snippet(self):
stop_words = 'hi\nalert'
self.request.set('stop_words', stop_words)
self.document.text = 'Some text that contains an alert and more'
text_alert = getAdapter(
self.document,
IStringSubstitution,
name=u'text_alert'
)
self.assertNotEqual(text_alert().find('alert'), -1)
def test_no_snippet(self):
self.document.text = 'Some text that contains an alert and more'
text_alert = getAdapter(
self.document,
IStringSubstitution,
name=u'text_alert'
)
self.assertEqual(
text_alert(),
u'',
)